Source of TestArrayListRemove.java


  1: //TestArrayListRemove.java
  2: 
  3: import java.util.ArrayList;
  4: import java.util.Scanner;
  5: 
  6: public class TestArrayListRemove
  7: {
  8:     private static Scanner keyboard = new Scanner(System.in);
  9: 
 10:     public static void main(String[] args)
 11:     {
 12:         ArrayList<Integer> a = new ArrayList<Integer>();
 13:         for (int i=1; i<=5; i++) a.add(i*i);
 14:         for (Integer entry: a) System.out.print(entry + " ");
 15:         System.out.println();
 16:         System.out.print("1Press Enter to continue ... ");
 17:         keyboard.nextLine();
 18: 
 19:         a.remove(2);
 20:         for (Integer entry: a) System.out.print(entry + " ");
 21:         System.out.println();
 22:         System.out.print("2Press Enter to continue ... ");
 23:         keyboard.nextLine();
 24: 
 25:         a.remove(25);  //Causes a runtime error.
 26:         //a.remove((Integer)25);  //Must remove 25 like this.
 27:         for (Integer entry: a) System.out.print(entry + " ");
 28:         System.out.println();
 29:         System.out.print("3Press Enter to continue ... ");
 30:         keyboard.nextLine();
 31:     }
 32: }
 33: