Results 1 to 8 of 8

Thread: Using Stacks

  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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());
                      }
             }
    }

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Just tested it. Works fine but I have a questions.

    Java Code:
    System.out.println(newStack.get(1));
    This works perfectly fine thus violating the stacks principle.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Reason is this. Even you use this stack, all the elements are either in an Array or Linked list. So,

    Java 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.

  5. #5
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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,
    Java Code:
    pop()
    In get() you can indexing, if you want. Definition like this,

    Java Code:
    get(int index)
    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.

    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 :)

  7. #7
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks for the detailed response.
    You cleared my concepts .... Thanks mate...
    Keep posting :D

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 08-07-2007, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •