943,717 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1567
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 14th, 2009
0

Stack and Queue

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
weblover is offline Offline
131 posts
since Feb 2009
Jun 14th, 2009
0

Re: Stack and Queue

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!
Reputation Points: 10
Solved Threads: 4
Newbie Poster
rcollins is offline Offline
17 posts
since May 2009
Jun 14th, 2009
0

Re: Stack and Queue

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
Reputation Points: 10
Solved Threads: 1
Junior Poster
weblover is offline Offline
131 posts
since Feb 2009
Jun 14th, 2009
0

Re: Stack and Queue

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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
rcollins is offline Offline
17 posts
since May 2009
Jun 14th, 2009
0

Re: Stack and Queue

Look at the java tutorial on collections there is an entire trail there, here is a link: Collections trail
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jun 15th, 2009
0

Re: Stack and Queue

Click to Expand / Collapse  Quote originally posted by weblover ...
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
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 15th, 2009
0

Re: Stack and Queue

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Jun 15th, 2009
0

Re: Stack and Queue

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.
Last edited by weblover; Jun 15th, 2009 at 10:26 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster
weblover is offline Offline
131 posts
since Feb 2009
Jun 15th, 2009
0

Re: Stack and Queue

Easiest way to iterate thru all the objects in a Stack is like this:

Java Syntax (Toggle Plain Text)
  1. for (Object o : myStack) {
  2. System.out.println(o); // or whatever
  3. }

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Jun 15th, 2009
0

Re: Stack and Queue

thnx alot james for your help ..my stack contains strings ....i fill it with strings exp: t.push("abd") how can i iterate it like this ???
Reputation Points: 10
Solved Threads: 1
Junior Poster
weblover is offline Offline
131 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to sort HashMap ??
Next Thread in Java Forum Timeline: JSlider to create different number of lables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC