// Need to import the LinkedList class from java.util: import java.util.*; public class ListExample { public static void main (String[] argv) { originalExample (); // This variation has "tighter", "cleaner" code. variation1 (); // These variations use different data structures, or // different ways of using the data structures. variation2 (); variation3 (); variation4 (); // These variations use different loop options. variation5 (); variation6 (); variation7 (); variation8 (); variation9 (); } static void originalExample () { // Create an empty list: LinkedList intList = new LinkedList (); // Produce data and add each to list. for (int i=0; i < 10; i++) { // Compute i-th square number. int square = i*i; intList.add (square); } // Print. for (int i=0; i < 10; i++) { int element = intList.get (i); System.out.println ("Element #" + i + " = " + element); } } static void variation1 () { // Create an empty list: LinkedList intList = new LinkedList (); // Produce data and add each to list. for (int i=0; i < 10; i++) { intList.add (i*i); } // Print. for (int i=0; i < 10; i++) { System.out.println ("Element #" + i + " = " + intList.get(i)); } } static void variation2 () { // Create an empty list that can hold any kind of object: LinkedList intList = new LinkedList (); for (int i=0; i < 10; i++) { // Make an Integer object for each value and put that into the list: intList.add ( new Integer(i*i) ); // This is possible because Integer is derived from Object. } // Print. for (int i=0; i < 10; i++) { // To extract, we need to cast: Integer I = (Integer) intList.get (i); System.out.println ("Element #" + i + " = " + I); } } static void variation3 () { // Different kind of list: ArrayList intList = new ArrayList (); // Produce data and add each to list. for (int i=0; i < 10; i++) { intList.add (i*i); } // Print. for (int i=0; i < 10; i++) { System.out.println ("Element #" + i + " = " + intList.get(i)); } } static void variation4 () { // Plain old array: int[] intList = new int [10]; for (int i=0; i < 10; i++) { intList[i] = i*i; } // Print. for (int i=0; i < 10; i++) { System.out.println ("Element #" + i + " = " + intList[i]); } } static void variation5 () { // Create an empty list: LinkedList intList = new LinkedList (); // Using a while loop (not preferred): int i = 0; while (i < 10) { intList.add (i*i); i ++; } // Using a do-while (again, a for-loop is preferred): i = 0; do { System.out.println ("Element #" + i + " = " + intList.get(i)); i ++; } while (i < 10); // Rule of thumb: use a for-loop if possible. Then prefer while-loops. // The do-while should be a last resort. } static void variation6 () { // Create an empty list: LinkedList intList = new LinkedList (); // DON'T: declare a for-loop variable outside the loop. int i = 0; for (i=0; i < 10; i++) { intList.add (i*i); } // This is an alternative, simpler loop for collections of // objects. However, it doesn't give you the index variable, // which is why the output doesn't have a counter. for (Integer I: intList) { System.out.println ("Element: " + I); } } static void variation7 () { // Create an empty list: LinkedList intList = new LinkedList (); for (int i=0; i < 10; i++) { intList.add (i*i); } // Most data structures allow "iteration" through their contents // via so-called iterators. For lists, one can use // ListIterators, which we get from the list itself. ListIterator iter = intList.listIterator(); while (iter.hasNext()) { int k = iter.next(); System.out.println ("Element: " + k); } } static void variation8 () { LinkedList intList = new LinkedList (); for (int i=0; i < 10; i++) { intList.add (i*i); } // It's possible to mimic the for-loop with an iterator: for (ListIterator iter = intList.listIterator(); iter.hasNext(); ) { // Note shorter version with the "next()" directly in the println argument: System.out.println ("Element: " + iter.next()); } // This makes for a strange looking for-loop. Note the lack of the // third segment in the for-loop (Can you see why?). } static void variation9 () { LinkedList intList = new LinkedList (); for (int i=0; i < 10; i++) { intList.add (i*i); } // One can also use a more general type of Iterator called, // you guessed it, Iterator. Iterator iter = intList.iterator(); while (iter.hasNext()) { System.out.println ("Element: " + iter.next()); } } }