Results 1 to 16 of 16

Thread: Stacks

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default Stacks

    I wanted to bring up the topic of Java's stack class.

    Do you think its write that programmers have the ability to iterate through a stack?
    My IP address is 127.0.0.1

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What do you mean?
    freedom exists in the world of ideas

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    What I mean is should you be able to search through a stack without having to pop off all the data first.
    My IP address is 127.0.0.1

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    No, that is impossible.... (for me)

    But, by using toArray() method, i can search those elements.

    How about you?
    freedom exists in the world of ideas

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    You can search through one of Java's stacks with an iterator method because of how Java made their stacks. I believe this breaks down what a stack should be able to do. In a stack you should only be allowed to see the first element, and you should only be able to add or remove to the top of the stack.
    My IP address is 127.0.0.1

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    ....Yep....
    freedom exists in the world of ideas

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    This topic as become a topic of debate in the Java community. I was wondering what the opinion of this sites members was. Please don't comment if you don't have anything to add to this discussion, it just makes you look incompetent.
    My IP address is 127.0.0.1

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Yep means, i agree....

    it just makes you look incompetent.
    ....For you only....

    I've learn stack in C and Assembly.

    in C, nice and helpful to implement...
    in Assembly, i always use stack implementation
    (if all general registers are full)
    But for Java? i never use that on my application(just on exercises).

    What for?
    Can you show us a serious application that uses a stack? And that application is fully dependent on a stack implementation?

    Maybe stack in java will be useful if you will design a new higher level language compiler using java.
    freedom exists in the world of ideas

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    My topic was about the way sun has implemented stacks in their API. A stack should only let the user be able to see the first item int the stack, but Java lets you search through the stack and see whats in the middle of it. An example of this would be a stack calculator. Sorry this isn't the best example, but it works.
    My IP address is 127.0.0.1

  10. #10
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    A stack should only let the user be able to see the first item int the stack.
    That's an ideal and that theory was maintain in Assembly.

    but Java lets you search through the stack and see whats in the middle of it.
    I think Sun MicroSystems added that feature ( can now access anywhere) for the programmer(implementor) to test if the exact value inserted on that stack are correct or if it is in correct order and able to predict what's the next value to be poped-up ( or the last value to be poped-up) and that is now the advantage.

    And it is for debugging purposes only, other purpose makes the stack abused.
    freedom exists in the world of ideas

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    But you shouldn't need to debug like this. If you are then you have deeper problems with your stack.
    My IP address is 127.0.0.1

  12. #12
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If i will use an Iterator, then problem may occur if failed to do so.

    But by using toArray, it only returns a shadow copy of the stack. Its just to browse the content without banging the element on that stack.(value checking only)

    If something is wrong, only push and pop should be and recommended to use.

    I don't know the other safe options( if any )
    freedom exists in the world of ideas

  13. #13
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    My 2 cents...

    I believe there is a time and place for every idea. Yes stacks allow you to work with the top of the stack always but the main idea behind it is not that. The main idea is to maintain an order for doing something and then undoing it. And stacks are just a solution for this problem.
    But then, there are other problems to be solved out there. Sometimes after you build the stack you might need to know some information about it. Sometimes you might need to peek inside and make some decisions. The implementation in Java simply allows the programmer to use a versatile library class as he sees fit.
    By the way, there are programming languages out there that use stack manipulation at runtime in order to do optimizations and to offer "magical" features.
    Nothing scares me in programming and in life more than a dogma and a religious war :). The only rule is "there are no rules". Don't take a book definition of a fact and make it your "dogma". This will only make you turn a blind eye to other points of view and other possibilities.
    Just think about it. Isn't a stack implementation more useful and flexible and possibly a part of the solution for more problems if it allows you to know more about its internal state at any moment? Of course it is also easier to mess up :). But nothing stops you from writing a wrapper that only gives access to the basic stack functionality.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  14. #14
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I agree with you daniel also I just haven't thought of anytime when it would be useful to know whats in the stack. If I have to know then maybe I'm using the wrong thing.
    My IP address is 127.0.0.1

  15. #15
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    As an example just think about two threads:
    - thread one is using the stack in a classical way pushing and popping values on the stack
    -thread two monitors the stack and it is notified whenever the stack changes; when the stack changes it will go and calculate statistics about the stack content

    These two threads use the stack in different ways: one as stack, the other as a list.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  16. #16
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Ok thank you for the example I think that has helped my understanding of stacks.
    My IP address is 127.0.0.1

Similar Threads

  1. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM
  2. 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
  •