Source of ListLooping.java


  1: import java.util.Scanner;
  2: import java.io.File;
  3: import java.io.FileNotFoundException;
  4: import java.util.ArrayList;
  5: import java.util.List;
  6: import java.util.ListIterator;
  7: 
  8: /**
  9:  * A program demonstrating the ways of looping thru a List.
 10:  *
 11:  * @author Mark Young (A00000000)
 12:  */
 13: public class ListLooping {
 14: 
 15:     private static final int NUM_NUMBERS = 40;
 16:     private static final int NUM_PER_LINE = 4;
 17: 
 18:     /**
 19:      * Generate a list of random numbers and then print it out using
 20:      * different loop structures.
 21:      *
 22:      * @param args the command line arguments
 23:      */
 24:     public static void main(String[] args) {
 25:         // create variables
 26:         Scanner kbd = new Scanner(System.in);
 27:         List<Double> myNumbers = new ArrayList<>();
 28: 
 29:         // introduce yourself
 30:         System.out.println("\n"
 31:                 + "I'm going to generate a list of " + NUM_NUMBERS 
 32:                 + " random numbers.");
 33: 
 34:         // get file name from user (or die trying!)
 35:         for (int i = 0; i < NUM_NUMBERS; ++i) {
 36:             myNumbers.add(1000.0 * Math.random());
 37:         }
 38: 
 39:         // report the numbers
 40:         printList1(myNumbers);
 41:         printList2(myNumbers);
 42:         printList3(myNumbers);
 43:     }
 44:     
 45:     /**
 46:      * Print the elements of a list using a standard for loop.
 47:      *
 48:      * @param theList the list to print
 49:      */
 50:     public static void printList1(List<Double> theList) {
 51:         System.out.println("\n"
 52:                 + "Printing the list using a standard for loop:");
 53:         int thisLineHas = 0;
 54:         for (int i = 0; i < theList.size(); ++ i) {
 55:             if (thisLineHas == NUM_PER_LINE) {
 56:                 System.out.println();
 57:                 thisLineHas = 0;
 58:             }
 59:             System.out.print("\t" + theList.get(i));
 60:             ++thisLineHas;
 61:         }
 62:         System.out.println();
 63:         System.out.println();
 64:     }
 65: 
 66:     /**
 67:      * Print the elements of a list using a for-each loop.
 68:      *
 69:      * @param theList the list to print
 70:      */
 71:     public static void printList2(List<Double> theList) {
 72:         System.out.println("\n"
 73:                 + "Printing the list using a for-each loop:");
 74:         int thisLineHas = 0;
 75:         for (Double x : theList) {
 76:             if (thisLineHas == NUM_PER_LINE) {
 77:                 System.out.println();
 78:                 thisLineHas = 0;
 79:             }
 80:             System.out.print("\t" + x);
 81:             ++thisLineHas;
 82:         }
 83:         System.out.println();
 84:         System.out.println();
 85:     }
 86: 
 87:     /**
 88:      * Print the elements of a list using a list iterator.
 89:      *
 90:      * @param theList the list to print
 91:      */
 92:     public static void printList3(List<Double> theList) {
 93:         System.out.println("\n"
 94:                 + "Printing the list using a list iterator:");
 95:         int thisLineHas = 0;
 96:         ListIterator<Double> it = theList.listIterator();
 97:         while (it.hasNext()) {
 98:             if (thisLineHas == NUM_PER_LINE) {
 99:                 System.out.println();
100:                 thisLineHas = 0;
101:             }
102:             System.out.print("\t" + it.next());
103:             ++thisLineHas;
104:         }
105:         System.out.println();
106:         System.out.println();
107:     }
108: }