CS REG - Unit 7 - Part ONE
CS REG - Unit 7 - Part ONE
Consider the following problem: Read words from a text file and store the data in memory.
String[] allWords = new String[2500];
Problem: You don't know how many words the text file contains.
The array above may be enough to store all the words from Wordle(about 2300).
But what if the text file contains all of the works of Shakespeare?
• Hard to create an array of the appropriate size.
Luckily, there are other ways to store data besides in an array.
Lists
• list: a collection storing an ordered sequence of elements
• each element is accessible by a 0-based index
• a list has a size (number of elements that have been added)
• elements can be added to the front, back, or elsewhere
• in Java, a list can be represented as an ArrayList object
UNIT 7 - ARRAYLIST
7.1 - INTRODUCTION TO ARRAYLIST
7.2 - ARRAYLIST METHODS
7.3 - TRAVERSING ARRAYLISTS
7.4 - DEVELOPING ALGORITHMS USING ARRAYLISTS
7.5 - SEARCHING
7.6 - SORTING
7.7 - ETHICAL ISSUES AROUND DATA COLLECTION
Difference between Array and ArrayList
Array ArrayList
You don’t need to provide the length of the ArrayList when instantiate
Primitive Values disguised as Wrapper Class Objects (2.9)
● ArrayList objects are designed to only store references to objects, NOT primitive values. A
workaround is to use Wrapper classes, which store primitive values as objects.
2.9 Wrapper class
● For every primitive type in Java, there is a built-in object type called a wrapper class.
● The wrapper class for int is called Integer, and for double it is called Double.
● Sometimes you may need to create a wrapped object for a primitive type so that you can give
it to a method that is expecting an object.
2.9 Wrapper Class on your reference sheets
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.
• Why those numbers?
• - Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space.
In 32 bits of space with one bit used to represent the sign you can represent that many values.
}
Create more ArrayLists
● a) to store Boolean values
• checking length
int len = names.length;
int lenl = list.size();
• storing a value
names[0] = "Jessica";
list.add("Jessica");
• getting a value
String s = names[0];
String s = list.get(0);
Size of the ArrayList
int size( ): returns the number of elements in the list
System.out.println(a1.size( )); // 0
CODING LAB
trees.add(treeG);
trees.add(treeW);
trees.size(); //2
CODING LAB
0 1 2 3
trees.add(1, treeY);
0 1 2 0 1 2 3
0 1 2 3 0 1 2
0 1 2 3 0 1 2
0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3
0 1 2 3
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
Beau
0 1 2 3 4 5 6 7
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
JJ
0 1 2 3 4 5 6 7
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
Beau
0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
JJ
0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0 1 2 3 4 5
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
JJ
0 1 2 3 4 5
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0 1 2 3 4
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0 1 2 3 4
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ” 0
1
for (int index = 0; index < ourDogs.size(); index++){
2
if (ourDogs.get(index).equals(“JJ”){
3
ourDogs.remove(index);
4
}
5
}
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ” 0
1
for (int index = 0; index < ourDogs.size(); index++){
2
if (ourDogs.get(index).equals(“JJ”){
3
ourDogs.remove(index);
4
index--;
5
}
}
0 1 2 3
Remove ALL Occurrences of “JJ”
0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0
int index = 0;
1
while (index < target.size()){
2
if (target.get(index).equals("JJ")){
3
target.remove(index);
4
}else{
5
index++;}
}
0 1 2 3
Lab time: findLargest(), doubleVal()
● findLargest(ArrayList<Integer> ):
● doubleVal(ArrayList<Integer>):
● doubleVal(ArrayList<Integer>):