Source of TestArrayList.java


  1: //TestArrayList.java
  2: //Shows two ArrayList constructors (default, and one parameter)
  3: //as well as several ways to display the values, and uses of
  4: //the following ArrayList methods:
  5: //size()
  6: //add() //two versions
  7: //remove()
  8: //set()
  9: //contains()
 10: //indexOf()
 11: //clear()
 12: //isEmpty()
 13: 
 14: import java.util.ArrayList;
 15: import java.util.Scanner;
 16: 
 17: public class TestArrayList
 18: {
 19:     private static Scanner keyboard = new Scanner(System.in);
 20: 
 21:     public static void main(String[] args)
 22:     {
 23:         //Default initial capacity will be 10.
 24:         //ArrayList<Integer> a = new ArrayList<Integer>();
 25:         //Initial capacity can be specified ...
 26:         ArrayList<Integer> a = new ArrayList<Integer>(20);
 27: 
 28:         //Size will be 0 (not the same as capacity).
 29:         System.out.println("Size of new ArrayList = " + a.size());
 30:         System.out.print("1Press Enter to continue ... ");
 31:         keyboard.nextLine();
 32: 
 33:         //Add five values and check size again.
 34:         for (int i=1; i<=5; i++) a.add(i);
 35:         System.out.println("Size after adding 5 values = " + a.size());
 36:         System.out.print("2Press Enter to continue ... ");
 37:         keyboard.nextLine();
 38: 
 39:         //Display all values in three different ways ...
 40:         for (int i=0; i<a.size(); i++) System.out.print(a.get(i) + " ");
 41:         System.out.println();
 42:         for (Integer entry: a) System.out.print(entry + " ");
 43:         System.out.println();
 44:         for (int entry: a) System.out.print(entry + " ");
 45:         System.out.println();
 46:         System.out.print("3Press Enter to continue ... ");
 47:         keyboard.nextLine();
 48: 
 49:         //Insert a new value at index 2 and display again.
 50:         a.add(2, 555);
 51:         for (Integer entry: a) System.out.print(entry + " ");
 52:         System.out.println();
 53:         System.out.print("4Press Enter to continue ... ");
 54:         keyboard.nextLine();
 55: 
 56:         //Remove value at index 2 and display again.
 57:         a.remove(2);
 58:         for (Integer entry: a) System.out.print(entry + " ");
 59:         System.out.println();
 60:         System.out.print("5Press Enter to continue ... ");
 61:         keyboard.nextLine();
 62: 
 63:         //Set new value at index 2 and display again.
 64:         a.set(2, 555);
 65:         for (Integer entry: a) System.out.print(entry + " ");
 66:         System.out.println();
 67:         System.out.print("6Press Enter to continue ... ");
 68:         keyboard.nextLine();
 69: 
 70:         //Check if an integer value and then an arbitrary object
 71:         //(in this case, a string) are in the ArrayList.
 72:         System.out.println(a.contains(4));
 73:         System.out.println(a.contains(6));
 74:         System.out.println(a.contains("Hello, world!"));
 75:         System.out.print("7Press Enter to continue ... ");
 76:         keyboard.nextLine();
 77: 
 78:         //Find where in the ArrayList an element is located.
 79:         //(in this case, a string) are in the ArrayList.
 80:         System.out.println(a.indexOf(4));
 81:         System.out.println(a.indexOf(6));
 82:         System.out.println(a.indexOf("Hello, world!"));
 83:         System.out.print("8Press Enter to continue ... ");
 84:         keyboard.nextLine();
 85: 
 86:         //Clear the ArrayList, then check to make sure it's empty.
 87:         //(in this case, a string) are in the ArrayList.
 88:         a.clear();
 89:         System.out.println(a.isEmpty());
 90:         System.out.print("9Press Enter to continue ... ");
 91:         keyboard.nextLine();
 92:     }
 93: }
 94: