Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8345280

java.util.Stack iterator not iterating from top of stack

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      // stack iterator starts from bottom of stack instead of top of stack
      import java.util.Stack;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Iterator;
      class Main {
          public static void main(String[] args) {
              Stack<Character> stack = new Stack<>();
              Collection<Character> list = new ArrayList<>();
              stack.push('a');
              list.add('b');
              stack.addAll(list);
              Iterator stackIterator = stack.iterator();
              // prints 'a' (bottom of stack) and not 'b' (top of stack)
              System.out.println(stackIterator.next());
              // prints 'b' (top of stack) and not 'a' (bottom of stack)
              System.out.println(stackIterator.next());
          }
      }


            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: