Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007, 11:27 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 11:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Something like this, a simple stack

Code:
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-27-2007, 05:08 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Just tested it. Works fine but I have a questions.

Code:
System.out.println(newStack.get(1));
This works perfectly fine thus violating the stacks principle.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2007, 05:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Reason is this. Even you use this stack, all the elements are either in an Array or Linked list. So,

Code:
newStack.get(1)
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)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2007, 10:21 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-28-2007, 10:36 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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,
Code:
pop()
In get() you can indexing, if you want. Definition like this,

Code:
get(int index)
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)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-28-2007, 10:50 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks for the detailed response.
You cleared my concepts .... Thanks mate...
Keep posting
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-28-2007, 10:53 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,403
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to be able to do this with stacks and queues as well as with vectors carl New To Java 1 08-07-2007 08:05 AM


All times are GMT +3. The time now is 11:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org