// This class includes sample client code for manipulating lists and sets. // // translation from array to ArrayList: // String[] => ArrayList // new String[10] => new ArrayList() // a.length => list.size() // a[i] => list.get(i) // a[i] = value; => list.set(i, value); // new list operations: // list.remove(i); --remove the ith value // list.add(value); --appends a value // list.add(i, value); --adds at an index // list.clear() --remove all value import java.util.*; import java.util.*; public class ListExample { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("four"); list.add("skip"); list.add("score"); list.add("seven"); list.add("wait...what was next?"); list.add("skip"); list.add("years"); list.add("ago"); list.add("skip"); list.add(3, "and"); list.remove(5); //System.out.println("list = " + list); //System.out.println(list.indexOf("seven")); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); for (String str : list) { System.out.print(str + " "); } System.out.println(); // hasNext // next Iterator iter = list.iterator(); while (iter.hasNext()) { String word = iter.next(); if (word.equals("skip")) { iter.remove(); } else { System.out.print(word + " "); } } System.out.println(); } }