Upload
jonah-miller
View
214
Download
2
Embed Size (px)
Citation preview
CSC 142 J(part 1) 1
CSC 142
The ArrayList class
[Reading: chapter 10]
The ArrayList class
Available in the package java.util. An ArrayList object stores a list of items. The
ArrayList automatically manages its size. The list grows as more items are added, or shrinks as some items are removed.
Items stored in an ArrayList all have the same reference type (i.e. defined by a class). Primitive type values can’t be stored as such in an ArrayList -> must use a wrapper type.
CSC 142 J(part 1) 2
Declaring an ArrayList object A named collection of data items of the same reference type. To declare an ArrayList of Strings, write
ArrayList<String> phrases;
To declare an ArrayList of integers, writeArrayList<Integer> numbers;
NOT ArrayList<int> numbers;
Always use the <> notation (= generics). At this point, no memory is reserved for the ArrayList
CSC 142 J(part 1) 3
Initializing an ArrayList To initialize an ArrayList (once declared)
phrases = new ArrayList<String>();numbers = new ArrayList<Integer>();
Other constructors are available e.g. // Make a copy of an existing ArrayList copy = new ArrayList<String>(phrases); // Or if you know the size you need daysOfTheWeek = new ArrayList<String>(7);
// But daysOfTheWeek is NOT limited // to 7 elements
CSC 142 J(part 1) 4
CSC 142 J(part 1) 5
Manipulating ArrayList elements (1) To add an element to an ArrayListArrayList<String> a = new ArrayList<String>();a.add(“Monday”);
a.add(“Wednesday”);
To access an ArrayList element, use its index (also called subscript), e.g.,
String first = a.get(0); // MondayString second = a.get(1); // Wednesday
Indices start at 0. If an ArrayList has 10 elements, the first element has index 0 and the last element has index 9.
• To insert in an ArrayList a.add(1,“Tuesday”); //Wednesday is now at index 2
CSC 142 J(part 1) 6
Manipulating ArrayList elements (2)
To remove an element from an ArrayList String s = a.remove(1); // element at index 1 is // removed automatically, elements at indices > 1
// are shifted down
// Also:
boolean success = a.remove(s); // removes s (= some object) if present. Returns true
// if s has been removed, and false if not.
Other operations:indexOf, contains, set
See java documentation
CSC 142 J(part 1) 7
ArrayList and loops (1) Loop with an index e.g. if a is an ArrayList of Rectangle objects for(int i = 0; i < a.size(); i ++) {
Rectangle r = a.get(i);
// work with r...
}
Loop with an iterator Iterator<Rectangle> it = a.iterator();
// could also use a ListIterator
while(it.hasNext()) {
Rectangle r = it.next();
// work with r...
}
Number of elements in a
ArrayList and loops (2) for each loop for(Rectangle r : a) {
// work with r...
}
Which one to use? To read the elements in no special order,
use a for each loop. To iterate through the ArrayList in a special
way (e.g. from last to first, every other element), use a loop with an index.
for each and iterator loops are read only.CSC 142 J(part 1) 8
CSC 142 J(part 1) 9
Memory view
ArrayList<String> a= new ArrayList<String>(); a.add(“Mercury”); a.add(“Earth”); a.add(1,”Venus”);
a
0
1
2
ArrayList
String
“Mercury”
String
“Venus”
String
“Earth”