Array List
Array List
Array List
Array
array: An object that stores many values of the same type.
element: One value in an array.
index: A 0-based integer to access an element from an array.
length:
Number of elements in the array.
index
value
12
49
-2
26
17
-6
84
72
element 0
element 4
length = 10
element 9
Array declaration
type[] name = new type[length];
Length explicitly provided. All elements' values initially 0.
int[] numbers = new int[5];
index
value
17 -6
// access
// modify
12
49
-2
88
17
-6
5
Array as param/return
public static void name(type[] name) {
public static type[] name(params)
// param
// return
Example:
public static int[] stutter(int[] a) {
int[] result = new int[a.length * 2];
for (int i = 0; i < result.length; i++) {
result[i] = a[i / 2];
}
return result;
}
Call:
int[] nums = {2, -4, 7};
int[] result = stutter(nums);
// {2, 2, -4, -4, 7, 7}
6
Description
binarySearch(array, value)
copyOf(array, length)
equals(array1, array2)
fill(array, value)
sort(array)
toString(array)
Exercise
Write a program that reads a file and displays
the words of that file as a list.
Naive solution
String[] allWords = new String[1000];
int wordCount = 0;
Scanner input = new Scanner(new File("data.txt"));
while (input.hasNext()) {
String word = input.next();
allWords[wordCount] = word;
wordCount++;
}
Problem: You don't know how many words the file will have.
Hard to create an array of the appropriate size.
Later parts of the problem are more difficult to solve.
Naive solution
String[] allWords = new String[1000];
int wordCount = 0;
Scanner input = new Scanner(new File("data.txt"));
while (input.hasNext()) {
String word = input.next();
allWords[wordCount] = word;
wordCount++;
}
Problem: You don't know how many words the file will have.
Hard to create an array of the appropriate size.
Later parts of the problem are more difficult to solve.
Collections
collection: an object that stores data; a.k.a. "data structure"
the objects stored are called elements
some collections maintain an ordering; some allow duplicates
typical operations: add, remove, clear, contains (search), size
examples found in the Java class libraries:
ArrayList, LinkedList, HashMap, TreeSet, PriorityQueue
11
12
Lists
list: a collection storing an ordered sequence of elements
13
Idea of a list
Rather than creating an array of boxes, create an object that
represents a "list" of items. (initially an empty list.)
[]
The list object keeps track of the element values that have
been added to it, their order, indexes, and its total size.
Think of an "array list" as an automatically resizing array object.
Internally, the list is implemented using an array and a size field.
14
add(index, value) inserts given value just before the given index,
shifting subsequent values to the right
clear()
indexOf(value)
get(index)
remove(index)
set(index, value)
size()
toString()
ArrayList methods 2
addAll(list)
addAll(index, list)
contains(value)
containsAll(list)
returns true if this list contains every element from given list
equals(list)
iterator()
listIterator()
lastIndexOf(value)
remove(value)
removeAll(list)
removes any elements found in the given list from this list
retainAll(list)
removes any elements not found in given list from this list
subList(from, to)
toArray()
16
17
18
storing a value
names[0] = "Jessica";
list.add("Jessica");
retrieving a value
String s = names[0];
String s = list.get(0);
19
Other exercises
Write a method reverse that reverses the order of the
elements in an ArrayList of strings.
Write a method capitalizePlurals that accepts an
ArrayList of strings and replaces every word ending with an
"s" with its uppercased version.
21
Exercise, revisited
Write a program that reads a file and displays
the words of that file as a list.
22
ArrayList as parameter
public static void name(ArrayList<Type> name) {
Example:
// Removes all plural words from the given list.
public static void removePlural(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if (str.endsWith("s")) {
list.remove(i);
i--;
}
}
}
ArrayList of primitives?
The type you specify when creating an ArrayList must be an
object type; it cannot be a primitive type.
// illegal -- int cannot be a type parameter
ArrayList<int> list = new ArrayList<int>();
25
Wrapper classes
Primitive Type Wrapper Type
int
Integer
double
Double
char
Character
boolean
Boolean
28