Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP CS Workshop 20081 ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.

Similar presentations


Presentation on theme: "AP CS Workshop 20081 ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data."— Presentation transcript:

1 AP CS Workshop 20081 ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data in a single collection that can be referred to with a single variable.

2 AP CS Workshop 20082 ArrayList An ArrayList is a sequence of objects that grows and shrinks as needed. The ArrayList class is part of the java.util package of the Java standard class library. –must import java.util.ArrayList

3 AP CS Workshop 20083 ArrayList Each element in the sequence can be accessed separately. We can explicitly overwrite an object at a specified position in the sequence, thus changing its value. We can inspect the object at a specified location in the sequence. We can add an object into a specified position of the sequence. We can add an object to the end of the sequence. We can remove an object from a specified location in the sequence.

4 AP CS Workshop 20084 java.util.ArrayList (implements List) ArrayList is generic ArrayList

5 AP CS Workshop 20085 ArrayList s for AP CS A Notice that the AP CS A Subset does not require the knowledge that ArrayList implements List.

6 AP CS Workshop 20086 java.util.ArrayList for AP CS A int size() // returns the number of elements in this list boolean add(E x) // appends x to the end of list; returns true E get(int index) // returns the element at the specified position in this list. E set(int index, E x) // replaces the element at index with x // returns the element formerly at the specified position

7 AP CS Workshop 20087 class java.util.ArrayList for AP CS A void add(int index, E x) // inserts x at position index, sliding elements // at position index and higher to the right // (adds 1 to their indices) and adjusts size E remove(int index) // removes element from position index, sliding // subsequent elements to the left (subtracts 1 from their // indices) and adjusts size // returns the element at the specified position in this list.

8 AP CS Workshop 20088 Course Description AP CS AB Notice that the AB Subset DOES require the knowledge that ArrayList implements List. iterators SHOULD BE TAUGHT in A

9 AP CS Workshop 20089 An example: import java.util.ArrayList; public class ArrayList_01 { public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add(new String("Dan")); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(int x = 0; x < aList.size(); x++) { System.out.println(aList.get(x)); }

10 AP CS Workshop 200810 Using enhanced FOR loop: import java.util.ArrayList; public class ArrayList_01_ForEach { public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add(new String("Dan")); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(String e:aList) { System.out.println(e); }

11 AP CS Workshop 200811 Other ArrayList methods // ArrayList_02 ArrayList students = new ArrayList (); students.add("Mary" ); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size());

12 AP CS Workshop 200812 What happens? ArrayList_02 ArrayList students = new ArrayList (); students.add("Mary"); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size()); MaryJamesKevin MaryTanyaJamesKevin temp MaryTanyaKevin MaryJohnKevin

13 AP CS Workshop 200813 An ArrayList is a sequence of objects. Array lists can hold any kind of object. For the generic ArrayList, you must include the type of object the ArrayList will hold. –ArrayList athletes = new ArrayList (); –ArrayList csci6 = new ArrayList (); –ArrayList accounts = new ArrayList (); The ArrayList method add adds an Object of the type specified in the array list declaration. The ArrayList method get returns an Object of the type specified in the array list declaration.

14 AP CS Workshop 200814 A bit more about toString()

15 AP CS Workshop 200815 What happens? public class StringTest { public static void main(String[] args) { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); }

16 AP CS Workshop 200816 WHY???? public class StringTest { public static void main(String[] args) { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); } [Fran, Marie, Joe] Let's look at the API!

17 AP CS Workshop 200817 The ArrayList is defined using generics. A generic is a method that is recompiled with different types as the need arises. List employeeList = new ArrayList (); Use of generics provides compile-time checking to make sure you are using the correct type Employee emp = employeeList.get(i).getName();

18 AP CS Workshop 200818 ArrayLists contain Objects Numbers are not objects. You can not have an ArrayList of int s or double s or boolean s or char s.

19 AP CS Workshop 200819 Again: An ArrayList is a sequence of objects. Array lists can hold any kind of object. The ArrayList method add adds an Object of specified type. The ArrayList method get returns an Object of specified type. What if we want our ArrayList to hold int s or double s?

20 AP CS Workshop 200820 Filling the ArrayList for (int k = 1; k <= 20; k++) { nbrs.add(new Integer(k)); }

21 AP CS Workshop 200821 Printing the ArrayList for(Integer e : nbrs) { System.out.println(e); } Integer has a toString method that does the right thing.

22 AP CS Workshop 200822 Printing the sum of the integer values in the ArrayList int sum = 0; Integer temp; for (int k = 0; k < nbrs.size(); k++) { temp = nbrs.get(k); sum += temp.intValue(); } System.out.println("sum equals: " + sum);

23 AP CS Workshop 200823 Printing the sum of the integer values in the ArrayList //alternate method using enhanced for and auto //boxing/unboxing sum = 0; for (Integer e:nbrs) { sum += e; } System.out.println("sum equals: " + sum);

24 AP CS Workshop 200824 Java 5.0 introduced Auto-boxing and Auto-unboxing (not tested) –The idea of auto-boxing and auto-unboxing is to make it easier to convert between primitive data types like int, double, and boolean and their equivalent classes, like Integer, Double, and Boolean. It is sometimes frustrating and tedious to have to do such a conversion, especially if results must be converted back to their original form.

25 AP CS Workshop 200825 Autoboxing/unboxing ArrayList numbers = new ArrayList (); numbers.add(14.5); numbers.add(3.4); double sum = numbers.get(0) + numbers.get(1);

26 AP CS Workshop 200826 Auto-boxing and Auto-unboxing Integer/int equality tests int == intOK Integer.equals(Integer)OK Integer == intOK Integer.equals(int)OK Integer.compareTo(int)==0OK

27 AP CS Workshop 200827 Auto-boxing and Auto-unboxing Integer/int equality tests –int.equals(Integer)NO –int.equals(int)NO –Integer == IntegerNO (as test for value equality) –Integer.equals(null)NO

28 AP CS Workshop 200828 Auto-boxing and Auto-unboxing NOT part of the AP CS Testable Subset. Students need to understand –intValue() –doubleValue()

29 AP CS Workshop 200829 ArrayList Applications Finding values, counting, max and min, average, median, mode….. Your students should be able to manipulate all sorts of algorithms for ArrayList s.

30 AP CS Workshop 200830 AP CS A 2007 Question 3a

31 AP CS Workshop 200831 AP CS A 2007 Question 3b

32 AP CS Workshop 200832 AP CS A 2007 Question 3b


Download ppt "AP CS Workshop 20081 ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data."

Similar presentations


Ads by Google