aspose file tools*
The moose likes Beginning Java and the fly likes Array Based Implementation of a Stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Based Implementation of a Stack" Watch "Array Based Implementation of a Stack" New topic
Author

Array Based Implementation of a Stack

Zak Tacc
Greenhorn

Joined: Feb 01, 2010
Posts: 25
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
Bartender

Joined: Oct 14, 2005
Posts: 18159
    
    8

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

Joined: Mar 19, 2008
Posts: 229
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.


God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Fidel: J2SE 1.3 API links? Seems a bit... old.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Based Implementation of a Stack
 
Similar Threads
Removing elements in an object
Inner classes
Stack implementations
Core Java Real time based questions
How is await() different from wait() ?