Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Based Implementation of a Stack

 
Zak Tacc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying to use an array based implementation of a stack, do I have to create my own push/pop/peek methods? or does java already have them?
 
Paul Clapham
Sheriff
Posts: 21120
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is not whether "java" has those methods. That's not even a meaningful question. If you are using an implementation of a stack, then the question is whether that implementation -- in particular the class which implements a stack -- has those methods. Which it should, if it's implementing a stack.

However if you are designing an implementation of a stack, then you should design the class so that it does have those methods.
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zak Tacc wrote:When trying to use an array based implementation of a stack, do I have to create my own push/pop/peek methods? or does java already have them?


You will have to create your Push/ POP methods. But there is a Stack class also that have pop and push methods.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Fidel: J2SE 1.3 API links? Seems a bit... old.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that Stack is terrible. Instead of using a Vector (a legacy class that should usually not be used) internally it instead extends it. Very bad design indeed. You should use a Deque instead; it even mentions this:
This interface should be used in preference to the legacy Stack class.

There is just one thing I would change: instead of using addFirst, removeFirst and peekFirst I'd use addLast, removeLast and peekLast. It's rarely more efficient to add elements to and remove elements from the start; best case (LinkedList) it's just as efficient, worst case (ArrayDeque) the entire internal structure needs to be reorganized. By working at the end you prevent this.

Hmmm. ArrayDeque is implemented differently than I expected. It in fact doesn't make a difference, just like LinkedList. So it doesn't matter at all from which side you work.
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Fidel: J2SE 1.3 API links? Seems a bit... old.


Yeah David, I understand it. I just want to give him a glimpse of Stck (Unfortunately, it was from older one :cry: ) .

Anyways Rob suggestions are great.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic