Lecture Videos
  1  import java.util.*;
  2  
  3  public class TestArrayListNew {
  4    public static void main(String[] args) {
  5      // Create a list to store cities
  6      ArrayList<String> cityList = new ArrayList<String>();
  7  
  8      // Add some cities in the list
  9      cityList.add("London");
 10      cityList.add("New York");
 11      cityList.add("Paris");
 12      cityList.add("Toronto");
 13      cityList.add("Hong Kong");
 14      cityList.add("Singapore");
 15  
 16      System.out.println("List size? " + cityList.size());
 17      System.out.println("Is Toronto in the list? " +
 18        cityList.contains("Toronto"));
 19      System.out.println("The location of New York in the list? "
 20        + cityList.indexOf("New York"));
 21      System.out.println("Is the list empty? " +
 22        cityList.isEmpty()); // Print false
 23  
 24      // Insert a new city at index 2
 25      cityList.add(2, "Beijing");
 26  
 27      // Remove a city from the list
 28      cityList.remove("Toronto");
 29  
 30      // Remove a city at index 1
 31      cityList.remove(1);
 32  
 33      // Display London Beijing Paris Hong Kong Singapore
 34      for (int i = 0; i < cityList.size(); i++)
 35        System.out.print(cityList.get(i) + " ");
 36      System.out.println();
 37  
 38      // Create a list to store two circles
 39      ArrayList<Circle> list = new ArrayList<Circle>();
 40  
 41      // Add a circle and a cylinder
 42      list.add(new Circle(2));
 43      list.add(new Circle(3));
 44  
 45      // Display the area of the first circle in the list
 46      System.out.println("The area of the circle? " +
 47        ((Circle)list.get(0)).getArea());
 48    }
 49  }