/** Author : Michael Robinson Program : arrayList_Iterators2.java Purpose : How to create a Java ArrayList using Iterators. Shows how to add elements to an ArrayList How to delete items How to get and print data items using for loops. Updated : July 2nd, 2012 */ import java.util.ArrayList; import java.util.Iterator; public class arrayList_Iterators2 { public static void alphaArrayList() { System.out.println("------------------------------------------"); System.out.println(" Processing ArrayList of String data type"); System.out.println(" using ITERATORS "); System.out.println("------------------------------------------"); System.out.println(" Creating ArrayList..."); //create an ArrayList object ArrayList arrayList = new ArrayList(); System.out.println(" Adding data elements to the ArrayList..."); //Add elements to Arraylist using arrayList.add("a"); arrayList.add("b"); arrayList.add("c"); arrayList.add("d"); arrayList.add("e"); arrayList.add("f"); System.out.println("\n Getting data elements from the ArrayList"); System.out.println(" Iterating through ArrayList elements..."); //This is a loop type class which allows to loop thru the ArrayList Iterator itr = arrayList.iterator(); //use hasNext() and next() methods of Iterator to iterate through the ArrayLis while(itr.hasNext()) { System.out.print(" "+itr.next()); } //now remove elements !!! always remove from the highest to the lowest location System.out.println("\n\n Removing data elements from the ArrayList on index 3 = " + arrayList.get(3) + "\n and index 1 = " + arrayList.get(1) ); arrayList.remove(3); arrayList.remove(1); System.out.println(); System.out.println(" Getting data elements from the ArrayList after removing"); System.out.println(" Iterating through ArrayList elements..."); itr = arrayList.iterator(); //reset iterator to beginning of ArrayList //use hasNext() and next() methods of Iterator to iterate through the ArrayLis while(itr.hasNext()) { System.out.print(" "+itr.next()); } System.out.println("\n\n"); }//end public static void alphaArrayList() public static void numericArrayList() { System.out.println("------------------------------------------"); System.out.println(" Processing ArrayList of Integer data type"); System.out.println(" using ITERATORS "); System.out.println("------------------------------------------"); System.out.println(" Creating ArrayList..."); //create an ArrayList object ArrayList arrayList = new ArrayList(); System.out.println(" Adding data elements to the ArrayList..."); //Add elements to Arraylist using arrayList.add(1100); arrayList.add(2100); arrayList.add(3100); arrayList.add(4100); arrayList.add(5100); arrayList.add(6100); System.out.println("\n Getting data elements in the ArrayList"); System.out.println(" Iterating through ArrayList elements..."); //This is a loop type class which allows to loop thru the ArrayList Iterator itr = arrayList.iterator(); //use hasNext() and next() methods of Iterator to iterate through the ArrayLis while(itr.hasNext()) { System.out.print(" "+itr.next()); } System.out.println("\n\n Removing data elements from the ArrayList on index 3 = " + arrayList.get(3) + "\n and index 1 = " + arrayList.get(1) ); //now remove some !!! always remove from the height to the lowest arrayList.remove(3); arrayList.remove(1); System.out.println("\n Getting data elements from the ArrayList after removing"); System.out.println(" Iterating through ArrayList elements..."); itr = arrayList.iterator(); //reset iterator to beginning of ArrayList //use hasNext() and next() methods of Iterator to iterate through the ArrayList while(itr.hasNext()) { System.out.print(" "+itr.next()); } System.out.println(); }//end public static void numericArrayList() public static void addNextRemoveIterator() { System.out.println("------------------------------------------"); System.out.println(" Processing ArrayList of String data type"); System.out.println(" using ITERATORS "); System.out.println("------------------------------------------"); System.out.println(" Creating ArrayList..."); //create an ArrayList object ArrayList arrayList = new ArrayList(); System.out.println(" Adding data elements to the ArrayList..."); //Add elements to Arraylist using arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); arrayList.add("F"); System.out.println("\n Getting data elements from the ArrayList"); System.out.println(" Iterating through ArrayList elements..."); //This is a loop type class which allows to loop thru the ArrayList Iterator itr = arrayList.iterator(); String temp = ""; //use hasNext() and next() methods of Iterator to iterate through the ArrayLis while( itr.hasNext() ) { if( itr.hasNext() ) { System.out.println( "\n\n itr.hasNext() = " + itr.hasNext() ); temp = itr.next(); System.out.println( " itr.next() = " + temp ); System.out.println( " removing = " + temp ); itr.remove(); } else { System.out.println( "\n ***The Arraylist is empty itr.hasNext() = [" + itr.hasNext() + "]" ); } } if( itr.hasNext() ) { System.out.println( "\n itr.hasNext() = " + itr.hasNext() ); } else { System.out.println( "\n itr.hasNext() = [" + itr.hasNext() + "]" ); System.out.println( " The Arraylist is empty itr.hasNext() = [" + itr.hasNext() + "]" ); } System.out.println(); System.out.println(" Getting data elements from the ArrayList after removing"); System.out.println(" Iterating through ArrayList elements..."); itr = arrayList.iterator(); //reset iterator to beginning of ArrayList //use hasNext() and next() methods of Iterator to iterate through the ArrayLis while(itr.hasNext()) { System.out.print(" "+itr.next()); } System.out.println("\n\n"); }//end public static void addNextRemoveIterator() public static void main(String[] args) { alphaArrayList(); numericArrayList(); addNextRemoveIterator(); } }