CP122 – CS I
Arrays and ArrayLists
Tech News!
AI company Vicarious develops Recursive Cortical Networks to crack captchas >50% of the time
Tech News!
AI company Vicarious develops Recursive Cortical Networks to crack captchas >50% of the time Walmart testing shelf-
scanning robots
Chapter 7: Arrays
● How can we store a bunch of data in a list?
● What operations can be performed on Java arrays?
Building a Soccer Roster
● Suppose we wanted to write a program to store the 100 meter sprinting speed of every player on a soccer team
● How would we do that? (SoccerSpeeds.java)
Isn't there a cleaner way to write SoccerSpeeds.java?
Java Arrays – Creating a new array
double[] speeds = new double[18];
Java Arrays – Creating a new array
double[] speeds = new double[18];
Data type of elements in the array.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Empty square brackets denote an array.
Without the brackets, the variable would look like a normal double.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Legal variable name. Same rules as regular variables.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Style note: use of plural names is a good thing to remind us that there are
multiple elements.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Use new keyword just like you would when creating a new object.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Repeated data type of elements in the array.
Java Arrays – Creating a new array
double[] speeds = new double[18];
Another set of square brackets with an integer value in between.
This value represents the size of the array (number of elements).
Java Arrays – Creating a new array
double[] speeds = new double[18];
The array's size is fixed/static. You cannot change the size of the array after
it has been created.
Java Arrays – Initializing an array
double[] speeds = {11.8, 12.6};
Java Arrays – Initializing an array
double[] speeds = {11.8, 12.6};
Instead of using new we can use curly braces and a comma-separated list of
initial values.
Java Arrays – Accessing an array element
speeds[0];
Java Arrays – Accessing an array element
speeds[0];
Start with the name of the array.
Java Arrays – Accessing an array element
speeds[0];
Then use square brackets and an integer.
This integer is the index of the element you want to access.
Java Arrays – Accessing an array element
speeds[0];
Arrays use an indexing scheme that starts at 0.
Java Arrays – Accessing an array element
speeds[0];
The first element in the array has index 0.
This is sometimes called the zeroth element
Java Arrays – Accessing an array element
speeds[?];
The last element in an array of size n...?
Java Arrays – Accessing an array element
speeds[n-1];
The last element in an array of size n...?
Java Arrays – Accessing an array element
System.out.println(speeds[0]);
Java Arrays – Accessing an array element
System.out.println(speeds[2000]);
What if the size of the array is 10?
Java Arrays – Changing the value an array element
speeds[0] = 10.3;
Java Arrays – Changing the value an array element
speeds[0] = 10.3;
Left-hand side of assignment operator is the same as array access.
Java Arrays – Changing the value an array element
speeds[0] = 10.3;
Right-hand size is new value. Can be any legal expression.
Java Arrays – Loop Access
for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);
}
Java Arrays – Loop Access
for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);
}
For loop starts at 0 (corresponding with the 0th element in the array).
Java Arrays – Loop Access
for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);
}
For loop continues while i is less than the length/size of the array.
Java Arrays – Loop Access
for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);
}
What is the value for i during the last execution of the loop?
Java Arrays – Loop Access
for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);
}
We can use the loop counter, i, as an index into our array.
Java Arrays in Memory
Address Value Array Access
0x12345670 13.2 myArray[0]
0x12345671 12.0 myArray[1]
0x12345672 16.2 myArray[2]
0x12345673 14.5 myArray[3]
0x12345674 14.2 myArray[4]
SoccerSpeeds.java with arrays
Building a Soccer Roster
● Suppose we don't know how many soccer players the user will want to enter
● What size should our array be?
Unknown number of players
SoccerPlayer[] players = new SoccerPlayer[1000];
Unknown number of players
SoccerPlayer[] players = new SoccerPlayer[1000];
No one will ever enter more than 1000 players, right?
Unknown number of players
SoccerPlayer[] players = new SoccerPlayer[1000];
Is this inefficient in any way?
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
ArrayList is a new Java class that we will use.
ArrayLists can change size dynamically.
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
Each ArrayList has a defined type of element (much like an array).
The < and > symbols surround the type stored in the ArrayList.
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
Here is the list variable name.
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
We need ArrayList<Type> on the right-hand side as well.
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
We also need empty parentheses...just like we would if we were calling a constructor with no input parameters.
Java ArrayLists – Creating a new ArrayList
ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();
Notice: no explicit length is required upon creation!
ArrayLists – Adding an element
players.add(new SoccerPlayer(“Terry”, 13.3));
SoccerPlayer myPlayer = new SoccerPlayer(“Terry”, 13.3);players.add(myPlayer);
OR
ArrayLists – Adding an element
players.add(new FootballPlayer(“Terry”, 200));
SoccerPlayer myPlayer = new SoccerPlayer(“Terry”, 13.3);players.add(myPlayer);
ORIn both cases add puts the new element on the end of the ArrayList.
This dynamically increases the length of the list.
ArrayLists – Accessing an element
players.get(0);
ArrayLists – Accessing an element
players.get(0);
Start with the name of the ArrayList.
ArrayLists – Accessing an element
players.get(0);
Use .get (a method call) instead of square brackets like we did with arrays.
ArrayLists – Accessing an element
players.get(0);
Put the index of the element you want to use inside parentheses.
ArrayLists - Changing the value of an element
players.set(2, new SoccerPlayer(“Terry”, 13.3));
ArrayLists - Changing the value of an element
players.set(2, new SoccerPlayer(“Terry”, 13.3));
Use the set method to change the value of an existing element.
ArrayLists - Changing the value of an element
players.set(2, new SoccerPlayer(“Terry”, 13.3));
The first parameter refers to the index of the element you want to change.
ArrayLists - Changing the value of an element
players.set(2, new SoccerPlayer(“Terry”, 13.3));
The second parameter is the new element value...in this case a new SoccerPlayer object.
ArrayLists - Removing an element
players.remove(2);
ArrayLists - Removing an element
players.remove(2);
Delete the element at index 2. If there are elements with higher indexes, then they shift down to fill the gap.
ArrayLists – Loop Access 1
for(int i = 0; i < players.size(); i++) {System.out.println(players.get(i));
}
ArrayLists – Loop Access 1
for(int i = 0; i < players.size(); i++) {System.out.println(players.get(i));
}
We use .size() to get the number of elements in the ArrayList.
Remember that this differs from arrays which use .length
ArrayLists – Loop Access 1
for(int i = 0; i < players.size(); i++) {System.out.println(players.get(i));
}
.get(i) grabs the ith element in the list.
ArrayLists – Loop Access 2
for (SoccerPlayer p : players) {System.out.println(p);
}
ArrayLists – Loop Access 2
for (SoccerPlayer p : players) {System.out.println(p);
}
Read this as: “for each SoccerPlayer, p, in the list players, print p”
SoccerRoster.java with ArrayLists
A Robot Invasion!
Let's make 100 Robot objects and add them to the screen!
Top Related