Java – ArrayList with for loop and iterator
David | December 12, 2009This is a simple example of an ArrayList with a for loop and an iterator. It loads 3 values into the ArrayList and then prints the values out using a for loop and an iterator.
ArrayList<Integer> mIntList = new ArrayList<Integer>(); mIntList.add(1); mIntList.add(2); mIntList.add(3); // This is a funny looking for loop because the 3rd parameter is not needed // the next() statement inside the for loop increments the iterator for(Iterator<Integer> mIterator = mIntList.iterator(); mIterator.hasNext(); /* none */) { //incrementing the iterator occurs here System.out.println(“value = ” + mIterator.next().toString()); }