// Hunter Schafer, CSE 143 // Some ArrayList examples import java.util.ArrayList; public class ArrayListClient { // Translation from array to ArrayList: // String[] ArrayList // new String[n] new ArrayList() // list[i] list.get(i) // list[i] = val list.set(i, val); // list.length list.size() // New operations: // list.add(val) appends val to list // list.add(i, val) inserts val at index i shifting subsequent values to the right // list.remove(i) removes and returns the value at i // list.clear() clears the entire ArrayList public static void main(String[] args) { //String[] array = new String[10]; ArrayList list = new ArrayList(); list.add("Hello"); list.add("Goodbye"); System.out.println(list); list.add(0, "cse 143 rocks!"); System.out.println(list); list.remove(1); System.out.println(list); // Here we made a ArrayList of integers // Doesn't work: ArrayList intList = new ArrayList(); ArrayList intList = new ArrayList(); intList.add(5); intList.add(14); System.out.println(intList); } }