/* Author : Michael Robinson Program : arrayListSimpleClass.java Purpose : How to create a Java ArrayList using for loops. Shows how to add elements to an ArrayList How to delete items How to get and print data items using for loops. Updated : June 29th, 2099 */ import java.util.ArrayList; public class arrayListSimpleClass { static String lines = "------------------------------------------"; public static void alphaArrayList() { System.out.println( lines ); System.out.println( " Processing ArrayList of String Data" + " Type" ); System.out.println(" Using for Loops" ); System.out.println( lines ); System.out.println( " Creating ArrayList..." ); //create an ArrayList object ArrayList arrayList = new ArrayList(); System.out.println( " Adding data elements to the" + " ArrayList..." ); //data is always added at the end of the Array List arrayList.add( "a" ); //adding a to Array List arrayList arrayList.add( "b" ); //adding b to Array List arrayList arrayList.add( "c" ); //adding c to Array List arrayList arrayList.add( "d" ); //adding d to Array List arrayList arrayList.add( "e" ); //adding e to Array List arrayList arrayList.add( "f" ); //adding fa to Array List arrayList System.out.println( "\n Getting data elements from " + "the ArrayList" ); //Use get method of ArrayList class to retrieve an element. //get( index ) returns element at the index in the ArrayList. //Notice that the command to retrieve an element/index from //and Array List is: arrayListName.get( location ) for( int x = 0; x < arrayList.size(); x++ ) { System.out.print( " " + arrayList.get(x) ); } //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); //always remove highest location first arrayList.remove(1); System.out.println( "\n Getting data elements from" + " ArrayList after removing" ); //arrayList.size() is equivalent to array.length in arrays for( int x = 0; x < arrayList.size(); x++ ) { System.out.print( " " + arrayList.get(x) ); } System.out.println( "\n" ); }//end public static void alphaArrayList() public static void numericArrayList() { System.out.println( lines ); System.out.println( " Processing ArrayList of Integer Data" + " Type" ); System.out.println( " Using for Loops" ); System.out.println( lines ); System.out.println( " Creating ArrayList..." ); //create an ArrayList object ArrayList arrayList = new ArrayList(); System.out.println( " Adding data elements to the" + " ArrayList..." ); //data is always added at the end of the Array List arrayList.add( 1100 ); //adding 1100 to Array List arrayList arrayList.add( 2100 ); //adding 2100 to Array List arrayList arrayList.add( 3100 ); //adding 3100 to Array List arrayList arrayList.add( 4100 ); //adding 4100 to Array List arrayList arrayList.add( 5100 ); //adding 5100 to Array List arrayList arrayList.add( 6100 ); //adding 6100 to Array List arrayList System.out.println( "\n Getting data elements in the" + " ArrayList" ); //Use get method of ArrayList class to retrieve an element. //get( index ) returns element at the index in the ArrayList. //Notice that the command to retrieve an element/index from //and Array List is: arrayListName.get( location ) for( int x = 0; x < arrayList.size(); x++ ) { System.out.print( " " + arrayList.get(x) ); } 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 location arrayList.remove(3); //always remove highest location first arrayList.remove(1); System.out.println( "\n Getting data elements from" + " ArrayList after remove" ); //arrayList.size() is equivalent to array.length in arrays for( int x = 0; x < arrayList.size(); x++ ) { System.out.print( " " + arrayList.get(x) ); } System.out.println( "\n" ); }//end public static void numericArrayList() public static void main( String arg[] ) { alphaArrayList(); numericArrayList(); }//end public static void main( String arg[] ) }//end public class arrayListSimpleClass