[Java] - How do I iterate a stack?

Discussion in 'OT Technology' started by INFERNO2K, Feb 15, 2008.

  1. INFERNO2K

    INFERNO2K Made in China

    Joined:
    Sep 29, 2001
    Messages:
    35,374
    Likes Received:
    1
    Location:
    Toronto
    Is this even possible?

    I have a stack

    Code:
    Stack<Object> t = shellHistory.getHistoryStack();
    I have an iterator

    Code:
    Iterator it = t.iterator();
    Code:
    while(it.hasNext()){
    Object a = it.next()
    //Processing to print out objects data
    }
    This code is just continually sticking the last object in the stack into object a. I think my understanding of iterating a stack isnt possible.

    What am I doing wrong?
     
  2. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    You don't want a stack, you want a vector. A stack is only used when you want to...well...stack things. You only ever put stuff on top of the stack, and you only ever take stuff off the top of the stack. If you want what's in the middle of the stack, you have to take off everything on top of it first.
     
  3. INFERNO2K

    INFERNO2K Made in China

    Joined:
    Sep 29, 2001
    Messages:
    35,374
    Likes Received:
    1
    Location:
    Toronto
    So in Java you cant iterate it regardless?
     
  4. b0b

    b0b OT Supporter

    Joined:
    Feb 17, 2004
    Messages:
    113
    Likes Received:
    0
    Location:
    Orlando, FL
    with a stack you push and pop. push puts an items on top of the stack. pop takes the top item off the stack. thats really all a stack can do.
     
  5. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    this bring back (painful) memories
     
  6. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    No, you can't iterate a stack, because the only publicly-accessible functions on a stack are push, pop, and peek (reading the last value without popping it first). The iterator object can't access the private portion of the stack, where all the pointers are stored, and so it can't traverse the stack. As far as the iterator can tell, the stack is just a single value, so it just shows you the one visible value at the top of the stack.

    For the record, I much prefer to write my own linked lists so I can do whatever I want. Back in college I wrote a single class that behaved like a vector, a queue, a deque (double-ended queue), a stack, or a plain old array, depending on which accessor functions I called. Never did get it to behave like a tree, though, but trees are special and it wouldn't have made much sense to use the same underlying node structure for a list and a tree anyway. My CS profs used to flip out over the fact that I actually had getPointer() and setPointer() functions that let me directly manipulate the array structure, but they couldn't fail me because my assignments never once crashed due to pointer errors. They really are easy to work with once you understand what they do.
     
    Last edited: Feb 15, 2008
  7. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    Code:
    
    import java.util.*;
    
    public class Test {
    
      public static void main( String args[] ) throws Exception {
        Stack s = new Stack();
        s.add("one");
        s.add("two");
        s.add("three");
    
        System.err.println(s);
    
        Iterator i = s.iterator();
    
        while(i.hasNext()) {
          System.err.println(i.next());
        }
      }
    }
    
    

    gives me:

    Code:
    [one, two, three]
    one
    two
    three
    
     

Share This Page