Results 1 to 8 of 8
Thread: Using Stacks
- 11-27-2007, 10:27 AM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
- 11-27-2007, 10:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Something like this, a simple stack
Java 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()); } } }
- 11-27-2007, 04:08 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Just tested it. Works fine but I have a questions.
This works perfectly fine thus violating the stacks principle.Java Code:System.out.println(newStack.get(1));
- 11-28-2007, 04:34 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.Java Code:newStack.get(1)
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.
- 11-28-2007, 09:21 AM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
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, 09:36 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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,Java Code:pop()
According to your code, you use get() to find the 2nd element of the stack. I told you that all the stack elements are in an array or linked list.Java Code:get(int index)
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 :)
- 11-28-2007, 09:50 AM #7
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Thanks for the detailed response.
You cleared my concepts .... Thanks mate...
Keep posting :D
- 11-28-2007, 09:53 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome pal. I love to give away that what I know for my friends.
So, your question is SOLVED. Nice...
Similar Threads
-
I want to be able to do this with stacks and queues as well as with vectors
By carl in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks