|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-27-2007, 11:27 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 115
|
|
|
Using Stacks
I want to use "Last Come First Serve" mechanism in my application. For that Stacks are the best choice. Some time back I implemented my own stack and can reuse that.
Does Java also provides Stack? Please give some examples.
Thanks.
|
|

11-27-2007, 11:45 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
|
|
Something like this, a simple stack
import java.util.Stack;
public class StacksDemo
{
public static void main(String[] arguments)
{
Stack newStack = new Stack();
newStack.push("10");
newStack.push("20");
newStack.push("30");
newStack.push("40");
// and many more items to push
while(!newStack.empty())
{
System.out.println(newStack.pop());
}
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on July 13, 2008)
|
|

11-27-2007, 05:08 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 115
|
|
Just tested it. Works fine but I have a questions.
System.out.println(newStack.get(1));
This works perfectly fine thus violating the stacks principle.
|
|

11-28-2007, 05:34 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
|
|
Reason is this. Even you use this stack, all the elements are either in an Array or Linked list. So,
what have done is just find the proper element.
Keep in mind that element index start from zero. If you start from zero to locate the elements you can see that stack principle not violated.
I think that is what you are talking about.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on July 13, 2008)
|
|

11-28-2007, 10:21 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 115
|
|
|
Thanks Eranga for the explanation.
When we POP, then the element is removed from the stack. I was assuming pop and get does the same job but its not the case.
|
|

11-28-2007, 10:36 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
|
|
No there is a different.
What the POP done is, removes the object at the top of the stack and returns that object as a value, simple value you PUSH. You can't indexing, and what you can do is POP values until the stack has at least one element or any other predefined condition.
Like this,
In get() you can indexing, if you want. Definition like this,
According to your code, you use get() to find the 2[sup]nd[/sup] element of the stack. I told you that all the stack elements are in an array or linked list.
Important thing to remember, you can use get() in different aspect, and my explanation is based on your code. If you can refer the documentation, you can find lots of explanations on it.
Hope this is helpful to you
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on July 13, 2008)
|
|

11-28-2007, 10:50 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Posts: 115
|
|
Thanks for the detailed response.
You cleared my concepts .... Thanks mate...
Keep posting 
|
|

11-28-2007, 10:53 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
|
|
|
You are welcome pal. I love to give away that what I know for my friends.
So, your question is SOLVED. Nice...
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on July 13, 2008)
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|