Transcript ArrayList

ArrayList
 Array’s are limited because we need to know the size
before we use them.
 An ArrayList is an extension of an array that grows
and shrinks as needed.
 The ArrayList class is part of the java.util
package of the Java standard class library.
1
ArrayLists contain Objects
 Numbers are not objects.
 Unlike an array, you can not have an ArrayList of
ints or doubles or booleans or chars.
2
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.
3
ArrayLists for AP CS A
Notice that the
AP CS A Subset
requires the
knowledge that
ArrayList
implements
List.
4
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
5
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.
6
An example:
import java.util.ArrayList;
public class ArrayListTest
{
public ArrayListTest
{
System.out.println("ArrayListTest");
ArrayList<String> aList = new ArrayList <String>();
aList.add("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));
}
}
}
7
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);
}
}
}
8
Other ArrayList methods
// ArrayListTest2
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());
9
What happens?
ArrayList_02
Mary
ArrayList<String> students = new
ArrayList<String>();
students.add("Mary");
students.add("James");
students.add("Kevin");
Mary
James
Tanya
Kevin
James
Kevin
students.add(1, "Tanya");
String temp = students.get(3);
System.out.println(temp);
temp
Mary
Tanya
Kevin
students.remove(2);
students.set(1, "John");
System.out.println(students.size());
Mary
John
Kevin
10
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.
11
What happens?
import java.util.ArrayList;
public class StringTest
{
public StringTest()
{
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Fran");
stringList.add("Marie");
stringList.add("Joe");
System.out.println(stringList);
}
}
12