/* * Kyle Pierce * CSE 143 * Client code using Java's ArrayList class */ import java.util.*; public class ArrayListClient { /* * Translation from array to ArrayList: * String[] arr ArrayList list * new String[n] new ArrayList() * arr[i] list.get(i) * arr[i] = val list.set(i, val) * arr.length list.size() * * New Operations: * list.add(val) appends the val to the list * list.add(i, val) inserts val at index i, * shifting subsequent values right * list.remove(i) removes and returns the value at i, * shifting subsequent values left * list.clear() empties the entire ArrayList */ public static void main(String[] args) { /* * String[] arr = new String[3]; * arr[0] = "welcome"; * arr[1] = "to"; * arr[2] = "143!"; * System.out.println(Arrays.toString(arr)); */ ArrayList list = new ArrayList(); list.add("welcome"); list.add("to"); list.add("143!"); System.out.println(); // add "hi" at the beginning list.add("hi"); System.out.println(list); // add "cse" before "143!" list.add("cse"); System.out.println(list); // remove the "to" list.remove(2); System.out.println(list); /* * Have to use wrapper types for primitives * Sadly ArrayList doesn't work :( */ ArrayList intList = new ArrayList(); intList.add(5); intList.add(1234); intList.add(42); System.out.println(intList); } }