-1

How to add int values and then iterate through stack getting LIFO order? Adding 7 and 1 returns 7 and 1.

   public void calculate_10to99() {

    Stack romanNumeralsStack =  new Stack();
        romanNumeralsStack.add(7);
        romanNumeralsStack.add(1);

        Iterator value = romanNumeralsStack.iterator();

    while (value.hasNext()){
        System.out.println(value.next());
    }
mat1
  • 83
  • 10
  • 3
    Use a `Deque`. Specifically, use an `ArrayDeque`. **Read** the [`Stack` Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html). It says (in part) *A more complete and consistent set of LIFO stack operations is provided by the [`Deque`](https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html) interface and its implementations, which should be used in preference to this class.* – Elliott Frisch Mar 23 '20 at 18:46
  • 2
    Why would you use a stack if you need FIFO order? – jrook Mar 23 '20 at 18:48
  • 3
    In the title of the question, you mentioned LIFO but in the description you want FIFO. Please clear the question. – js_248 Mar 23 '20 at 18:51
  • 1
    Don't use the iterator, just use romanNumeralsStack.push() instead of add() and pop() to remove and you should get LIFO – LocalhostNotfound Mar 23 '20 at 18:56
  • 1
    @oxy_js I think mat1 means that he is expecting LIFO order but getting FIFO order – LocalhostNotfound Mar 23 '20 at 18:57
  • FIFO was a mistake, its corrected now. – mat1 Mar 24 '20 at 00:53
  • Does this answer your question? [Java Collections (LIFO Structure)](https://stackoverflow.com/questions/302460/java-collections-lifo-structure) – Nastaran Hakimi Mar 24 '20 at 07:33

2 Answers2

2

Do it like this:

public void calculate_10to99() {

    Stack<Integer> romanNumeralsStack =  new Stack<>();
        romanNumeralsStack.push(7);
        romanNumeralsStack.push(1);

    while (!romanNumeralsStack.empty()){
        System.out.println(romanNumeralsStack.pop());
    }
 }

The iterator() method comes from its superclass Vector and just returns all of the elements in order, so iterator.next() does not give you LIFO

  • What type of instance does `romanNumeralsStack.pop()` returns? `System.out.println(romanNumeralsStack.pop())` pops and print the number from the stack, but `int temp = romanNumeralsStack.pop()` doesnt work, neither `Integer` or `String`. Only `Object` works. What type of object is popped from stack? – mat1 Mar 24 '20 at 01:02
  • 1
    For return type you need to define stack as Stack romanNumeralsStack = new Stack(); then only you can use int tmp = romanNumeralsStack.pop() – js_248 Mar 24 '20 at 01:12
1

You are using iterator as

Iterator value = romanNumeralsStack.iterator();

Which return element in random order from what was present in the stack.

You should use another method such as. This will check if the stack is not empty then it keeps on popping element from the top which makes it LIFO.

public static void calculate_10to99() {

        Stack romanNumeralsStack = new Stack();
        romanNumeralsStack.add(7);
        romanNumeralsStack.add(1);

        while (!romanNumeralsStack.isEmpty()){
            System.out.println(romanNumeralsStack.pop());

        }
    }
js_248
  • 2,032
  • 4
  • 27
  • 38