import java.util.ArrayList; public class ArrayListWithIntegerGenerics { public static void main(String[] args) { // Generics allows an ArrayList to be created by specifying the type // of element that will be stored in the list; it does limit the // ArrayList to contain only one type of element, but a feature // called autoboxing eliminates the need for the elements to be // cast into their specific type when they are retrieved from the // ArrayList, since the ArrayList will already know what type of // elements it is holding ArrayList list = new ArrayList(); list.add(7); // Adds 7 to ArrayList list.add(5); // Adds 5 to end of ArrayList System.out.println("Element #1: " + list.get(0)); System.out.println("Element #2: " + list.get(1)); System.out.println("List Size: " + list.size()); list.set(0, 3); // Changes first element to 3 System.out.println("\nElement #1: " + list.get(0)); System.out.println("Element #2: " + list.get(1)); System.out.println("List Size: " + list.size()); list.remove(0); // Removes first element in ArrayList System.out.println("\nElement #1: " + list.get(0)); System.out.println("List Size: " + list.size()); list.clear(); // Removes ALL elements from ArrayList System.out.println("\nList Size: " + list.size()); } }