import java.util.*; import java.io.*; public class ArrayListPractice { /* * 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 * list.indexOf(val) returns the index of the first occurence * of the given value in the list */ public static void main(String[] args) throws FileNotFoundException { ArrayList list = new ArrayList(); list.add("May"); list.add("the"); list.add("power"); list.add("be"); list.add("with"); list.add("you"); System.out.println(list); // Add "force" at index 2 list.add(2, "force"); // Remove "power" list.remove(3); System.out.println(list); // There's useful methods that can find elements for you too! System.out.println(list.indexOf("with")); // You can make ArrayList's of other types too! For primitives like ints // however, you need to use the "wrapper" class ArrayList numbers = new ArrayList(); numbers.add(5); numbers.add(26); numbers.add(14); System.out.println(numbers); } }