i looked up the api ....and there is no method that let's u output the element of a stack ....and for the Queue where can i find a ready class for it ....thnx for help
Oh, there isn't? Because by typing in "Stack java" to google I found this link . Hm
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Because a Stack can contain any kind of Objects, of unlimited complexity, there's no sensible way to provide a generic "print" method that would work usefully for everything that could be in the stack. Hence all the previous advice saying that you have to loop thru all the elements in the Stack printing each element in some way that makes sense for the type of each element.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Easiest way to iterate thru all the objects in a Stack is like this:
for (Object o : myStack) {
System.out.println(o); // or whatever
}
An interface is set of public methods that a class must implement. For example, the Queue interface specifies methods called element, offer, peek, poll, remove. If a class is declared as implements Queue then it must implement all these methods exactly as defined in the Queue interface. The compiler then knows that you can call peek, poll etc on members of that class. There are a number of classes on the API that implement the Queue interface, such as LinkedList or PriorityQueue - all of these have all the Queue methods, and thus be used to hold a queue.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Show me the declaration of your Stack - how you declare it affects how you use it.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
What exceptions? Where exactly?
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
That's OK, but I think rcollins deserves thanks too!
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073