publicTpop(){if(isEmpty()){thrownewEmptyStackException();}Titem=stack.pop();if(stack.isEmpty()){isEmpty=true;}returnitem;}publicbooleanisEmpty(){returnisEmpty;// This has O(1) constant time}
If you pop an item, it'll call the isEmpty() method, which returns the class variable isEmpty. If that's true, then it'll set the class variable to true. Isn't that tautological?
It feels like the isEmpty() method should wrap the size or count you're trying to avoid, else this won't work.
Learn something new every day.
- I am a senior software engineer working in industry, teaching and writing on software design, SOLID principles, DDD and TDD.
Location
Buenos Aires
Education
Computer Science Degree at Universidad de Buenos Aires
If you
popan item, it'll call theisEmpty()method, which returns the class variableisEmpty. If that'strue, then it'll set the class variable totrue. Isn't that tautological?It feels like the
isEmpty()method should wrap thesizeorcountyou're trying to avoid, else this won't work.Thank you very much
The main challenge is about readabality and not performance.
I'm not an expert in Java and I didn't know this facts so they are good points