AP CS Workshop 2008 1
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.
AP CS Workshop 2008 2
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
AP CS Workshop 2008 3
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.
AP CS Workshop 2008 4
java.util.ArrayList (implements List)
ArrayList is genericArrayList<String>ArrayList<Student>
ArrayList<BankAccount>
AP CS Workshop 2008 5
ArrayLists for AP CS ANotice that the AP CS A Subset does not require the knowledge that ArrayList implements List.
AP CS Workshop 2008 6
java.util.ArrayList<E>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
AP CS Workshop 2008 7
class java.util.ArrayList<E> 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.
AP CS Workshop 2008 8
Course Description AP CS AB
Notice that the AB Subset DOES require the knowledge that ArrayList implements List.iterators
SHOULD BE TAUGHT in A
AP CS Workshop 2008 9
An example:import java.util.ArrayList;
public class ArrayList_01{ public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList<String> aList = new ArrayList <String>(); 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)); } } }
AP CS Workshop 2008 10
Using enhanced FOR loop:import java.util.ArrayList;
public class ArrayList_01_ForEach{ public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList<String> aList = new ArrayList <String>(); aList.add(new String("Dan")); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:");
for(String e:aList)
{
System.out.println(e);
}}
}
AP CS Workshop 2008 11
Other ArrayList methods// ArrayList_02ArrayList<String> students = new ArrayList<String>();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());
AP CS Workshop 2008 12
What happens?ArrayList_02
ArrayList<String> students = new ArrayList<String>();
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());
Mary James Kevin
Mary Tanya James Kevin
temp
Mary Tanya Kevin
Mary John Kevin
AP CS Workshop 2008 13
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<Athlete> athletes = new ArrayList<Athlete>();
– ArrayList<Student> csci6 = new ArrayList<Student>();
– ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
• 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.
AP CS Workshop 2008 14
A bit more about
toString()
AP CS Workshop 2008 15
What happens?public class StringTest{
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Fran");stringList.add("Marie");stringList.add("Joe");
System.out.println(stringList);}
}
AP CS Workshop 2008 16
WHY????public class StringTest{
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Fran");stringList.add("Marie");stringList.add("Joe");
System.out.println(stringList);}
} [Fran, Marie, Joe] Let's look at the API!
AP CS Workshop 2008 17
The ArrayList is defined using generics .
• A generic is a method that is recompiled with different types as the need arises. List<Employee> employeeList = new ArrayList<Employee>();
• Use of generics provides compile-time checking to make sure you are using the correct type Employee emp = employeeList.get(i).getName();
AP CS Workshop 2008 18
ArrayLists contain Objects
• Numbers are not objects.
• You can not have an ArrayList of ints or doubles or booleans or chars.
AP CS Workshop 2008 19
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 ints or doubles?
AP CS Workshop 2008 20
Filling the ArrayList
for (int k = 1; k <= 20; k++)
{
nbrs.add(new Integer(k));
}
AP CS Workshop 2008 21
Printing the ArrayList
for(Integer e : nbrs)
{
System.out.println(e);
}
Integer has a toString method that does the right thing.
AP CS Workshop 2008 22
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);
AP CS Workshop 2008 23
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);
AP CS Workshop 2008 24
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.
AP CS Workshop 2008 25
Autoboxing/unboxing
ArrayList<Double> numbers = new ArrayList<Double>();
numbers.add(14.5);
numbers.add(3.4);
double sum = numbers.get(0) + numbers.get(1);
AP CS Workshop 2008 26
Auto-boxing and Auto-unboxing
• Integer/int equality tests
int == int OK
Integer.equals(Integer) OK
Integer == int OK
Integer.equals(int) OK
Integer.compareTo(int)==0 OK
AP CS Workshop 2008 27
Auto-boxing and Auto-unboxing
• Integer/int equality tests– int .equals(Integer) NO– int .equals(int) NO– Integer == Integer NO (as test for value
equality)
– Integer.equals(null) NO
AP CS Workshop 2008 28
Auto-boxing and Auto-unboxing
• NOT part of the AP CS Testable Subset.
• Students need to understand– intValue()– doubleValue()
AP CS Workshop 2008 29
ArrayList Applications
Finding values, counting, max and min, average, median, mode…..
Your students should be able to manipulate all sorts of algorithms for ArrayLists.
AP CS Workshop 2008 30
AP CS A 2007 Question 3a
AP CS Workshop 2008 31
AP CS A 2007 Question 3b
AP CS Workshop 2008 32
AP CS A 2007 Question 3b