Download presentation
Presentation is loading. Please wait.
Published byDakota Toft Modified over 9 years ago
2
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
3
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()
4
What can ArrayList hold?What can ArrayList hold? Object references No primitives! Primitives are Auto-Boxed - converted to corresponding class (int to Integer, etc.)
5
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
6
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"); }
7
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();
8
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); }
9
ArrayList intList = new ArrayList (); int n = 1; boolean addMore = true; while (addMore) { intList.add(10 * n); n++; if (n == 100) addMore = false; }
10
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
11
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.
12
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
13
ArrayList actors1 = new ArrayList (); // Good ArrayList actors2 = new ArrayList (); // BAD List actors3 = new ArrayList (); // Good List actors4 = actors1;// BAD
14
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”); }
15
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”); }
16
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”; }
17
public class SatSession2Roster { public static void main (String args[]) { ArrayList roster2 = new ArrayList (); }
18
Multiple Choice QuestionsMultiple Choice Questions
19
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()
20
Sample Free ResponseSample Free Response
21
Solution to FR Question PART A: public int getTotalBoxes() { int sum = 0; for (CookieOrder co : this.orders) { sum += co.getNumBoxes(); } return sum; }
22
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; }
23
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.