| | |
Stack and Queue
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 98
Reputation:
Solved Threads: 1
hi all ...how are u? i have a question ...how can i output the items of a java Stack ...is there any predifine method??
and another question ...how can i use the class Queue without interface ? ...because i'm typing in my code (Queue q =new Queue()
but it's giving me an exception ...what should i do ??
thnx alot in advance.
and another question ...how can i use the class Queue without interface ? ...because i'm typing in my code (Queue q =new Queue()

but it's giving me an exception ...what should i do ??
thnx alot in advance.
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 3
My suggestion is to google Java API. http://java.sun.com/j2se/1.5.0/docs/api/
Look up Stack. It will list all the methods available. It will also show you other methods inherited from other classes. Of interest to you till be List which has iterators which will allow you you iterate through the elements of the Stack.
Look up Queue. It is nothing more than an interface so either you have to implement a Queue or choose a class that implements one for you. When you look up Queue it actually list all known implementing classes one of these classes might work better for you.
Give this a shot and come back with any questions. Gook luck!
Look up Stack. It will list all the methods available. It will also show you other methods inherited from other classes. Of interest to you till be List which has iterators which will allow you you iterate through the elements of the Stack.
Look up Queue. It is nothing more than an interface so either you have to implement a Queue or choose a class that implements one for you. When you look up Queue it actually list all known implementing classes one of these classes might work better for you.
Give this a shot and come back with any questions. Gook luck!
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 3
For outputting the elements of a Stack you aren't going to use a method you are going to use iterators to go through the stack element by element. Do a search online to get some info about iterators. As for the Queue look at the API page. It has a list of Implementing classes. This means that these classes are effectivly queues. Use one of them if you don't want to implement the code yourself.
Last edited by rcollins; Jun 14th, 2009 at 6:54 pm.
Look at the java tutorial on collections there is an entire trail there, here is a link: Collections trail
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
•
•
•
•
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
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
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.
•
•
Join Date: Feb 2009
Posts: 98
Reputation:
Solved Threads: 1
thnx all ...but for the iterator ........when i can let the while stop ....i tried i can print them in a while but i don't know in what condition can i stop the while ....
and for interfaces ...i don't know how to use them ..i did not use them anytime ...can't i find a class queue without interface ?
i found out a way to print the stack elements ..but it removes all the element of it ...is there another way ?
thnx in advance.
and for interfaces ...i don't know how to use them ..i did not use them anytime ...can't i find a class queue without interface ?
i found out a way to print the stack elements ..but it removes all the element of it ...is there another way ?
thnx in advance.
Last edited by weblover; Jun 15th, 2009 at 10:26 am.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
Easiest way to iterate thru all the objects in a Stack is like this:
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
Java Syntax (Toggle Plain Text)
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. ![]() |
Similar Threads
- Help on stack , queue, palindrome program... (C++)
- at the moment this is a stack of queue, i am trying to make it into first in first ou (C++)
- Stack Queue Fstream (C++)
Other Threads in the Java Forum
- Previous Thread: How to sort HashMap ??
- Next Thread: JSlider to create different number of lables
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows







