Implements the List Interface What is an interface? all abstract methods can not have instance variables List is an interface ArrayList implements List What other interface have you seen? Grid
ArrayList MethodsArrayList Methods int size() boolean add(E obj) void add(int index, E obj) E get(int index) E set(int index, E obj) E remove(int index) void clear()
What can ArrayList hold?What can ArrayList hold? Object references No primitives! Primitives are Auto-Boxed - converted to corresponding class (int to Integer, etc.)
Advantages / Disadvantages Advantages Easier to manipulate Methods available to insert, delete, access Easily /automatically resized Disadvantages One dimensional only Can have ArrayLists of ArrayLists instead Less efficient – space and time
Define import java.util.ArrayList; public class arrayListExample { private ArrayList B ; public arrayListExample() { B = new ArrayList (); B.add("tomato"); B.add("pear"); B.add("durian"); B.add("apple"); B.add("mango"); }
Populating private ArrayList card; card.add(0,55); // add 55 before 0 index card.add(99); // add 99 at end of list Finding Length int clen = card.size();
Accessing elements in a loop for ( int i = 0; i < stringList.size(); i++) { String item = stringList.get(i); System.out.println("Item " + i + " : " + item); } for ( String item: stringList){ System.out.println("retrieved element: " + item); }
ArrayList intList = new ArrayList (); int n = 1; boolean addMore = true; while (addMore) { intList.add(10 * n); n++; if (n == 100) addMore = false; }
For Each LoopFor Each Loop Can’t be used to change an ArrayList object! List list = new ArrayList (); list.add("Hi"); … for (String s : list) s = "NO"; // does not change list objects
For Each LoopFor Each Loop Can be used to mutate a referenced object! List bugs = new ArrayList (); bugs.add(new Bug()); … for (Bug b : bugs) b.setColor(Color.GREEN);// bugs are now green Can’t add or remove inside For-Each loop! Gets ConcurrentModificationException.
Generic TypesGeneric Types Generic Types form a hierarchy (is-a). Hierarchy is based on the base type. Hierarchy is not based on the parameters to that type. ArrayList is-a List ArrayList is-not-a List
ArrayList actors1 = new ArrayList (); // Good ArrayList actors2 = new ArrayList (); // BAD List actors3 = new ArrayList (); // Good List actors4 = actors1;// BAD
Comparing List ElementsComparing List Elements ArrayList myList = new ArrayList (); // elements added to myList for (int idx=0; idx < myList.size(); i++) { if (myList.get(idx).equals(“monkey”) ) System.out.println(“Found a monkey”); }
Comparing List ElementsComparing List Elements ArrayList myList = new ArrayList (); // elements added to myList for (int idx=0; idx < myList.size(); i++) { if (myList.get(idx).compareTo(“monkey”) < 0) System.out.println(“Less than monkey”); if (myList.get(idx).compareTo(“monkey”) == 0) System.out.println(“Equals monkey”); if (myList.get(idx).compareTo(“monkey”) > 0) System.out.println(“More than monkey”); }
public class Student { private String name, school; private int grade; public Student( String nm, int gd, String sch) { //code to set all the instance variables} public String getName() { return name;} public int getGrade() { return grade;} public String getSchool() { return school;} public String toString() { return “Name: ” + getName() + “\n“ + “Grade: “ + getGrade() +“\n” + “School: “ + getSchool() + “\n\n”; }
public class SatSession2Roster { public static void main (String args[]) { ArrayList roster2 = new ArrayList (); }
Multiple Choice QuestionsMultiple Choice Questions
MC AnswersMC Answers Q1: D // if 2 ones are next to each other the second one gets skipped. Q2: D Q3: B // one of the methods was a set() not an add()
Sample Free ResponseSample Free Response
Solution to FR Question PART A: public int getTotalBoxes() { int sum = 0; for (CookieOrder co : this.orders) { sum += co.getNumBoxes(); } return sum; }
PART B:PART B: public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; for (int i = this.orders.size() - 1; i >= 0; i--) { if (cookieVar.equals(this.orders.get(i).getVariety())) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } return numBoxesRemoved; }
Alternative solution (forward traversal direction): public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; int i = 0; while (i < this.orders.size()) { if (cookieVar.equals(this.orders.get(i).getVariety()) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } else i++; } return numBoxesRemoved; }