66
CP122 – CS I Arrays and ArrayLists

CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

CP122 – CS I

Arrays and ArrayLists

Page 2: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Tech News!

AI company Vicarious develops Recursive Cortical Networks to crack captchas >50% of the time

Page 3: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Tech News!

AI company Vicarious develops Recursive Cortical Networks to crack captchas >50% of the time Walmart testing shelf-

scanning robots

Page 4: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Chapter 7: Arrays

● How can we store a bunch of data in a list?

● What operations can be performed on Java arrays?

Page 5: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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)

Page 6: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Isn't there a cleaner way to write SoccerSpeeds.java?

Page 7: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Creating a new array

double[] speeds = new double[18];

Page 8: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Creating a new array

double[] speeds = new double[18];

Data type of elements in the array.

Page 9: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 10: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Creating a new array

double[] speeds = new double[18];

Legal variable name. Same rules as regular variables.

Page 11: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 12: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Creating a new array

double[] speeds = new double[18];

Use new keyword just like you would when creating a new object.

Page 13: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Creating a new array

double[] speeds = new double[18];

Repeated data type of elements in the array.

Page 14: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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).

Page 15: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 16: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Initializing an array

double[] speeds = {11.8, 12.6};

Page 17: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 18: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[0];

Page 19: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[0];

Start with the name of the array.

Page 20: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 21: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[0];

Arrays use an indexing scheme that starts at 0.

Page 22: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[0];

The first element in the array has index 0.

This is sometimes called the zeroth element

Page 23: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[?];

The last element in an array of size n...?

Page 24: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

speeds[n-1];

The last element in an array of size n...?

Page 25: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

System.out.println(speeds[0]);

Page 26: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Accessing an array element

System.out.println(speeds[2000]);

What if the size of the array is 10?

Page 27: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Changing the value an array element

speeds[0] = 10.3;

Page 28: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Changing the value an array element

speeds[0] = 10.3;

Left-hand side of assignment operator is the same as array access.

Page 29: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Changing the value an array element

speeds[0] = 10.3;

Right-hand size is new value. Can be any legal expression.

Page 30: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java Arrays – Loop Access

for(int i = 0; i < speeds.length; i++) {System.out.println(speeds[i]);

}

Page 31: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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).

Page 32: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 33: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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?

Page 34: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 35: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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]

Page 36: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

SoccerSpeeds.java with arrays

Page 37: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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?

Page 38: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Unknown number of players

SoccerPlayer[] players = new SoccerPlayer[1000];

Page 39: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Unknown number of players

SoccerPlayer[] players = new SoccerPlayer[1000];

No one will ever enter more than 1000 players, right?

Page 40: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Unknown number of players

SoccerPlayer[] players = new SoccerPlayer[1000];

Is this inefficient in any way?

Page 41: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java ArrayLists – Creating a new ArrayList

ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();

Page 42: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 43: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 44: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java ArrayLists – Creating a new ArrayList

ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();

Here is the list variable name.

Page 45: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java ArrayLists – Creating a new ArrayList

ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();

We need ArrayList<Type> on the right-hand side as well.

Page 46: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 47: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

Java ArrayLists – Creating a new ArrayList

ArrayList<SoccerPlayer> players = new ArrayList<SoccerPlayer>();

Notice: no explicit length is required upon creation!

Page 48: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Adding an element

players.add(new SoccerPlayer(“Terry”, 13.3));

SoccerPlayer myPlayer = new SoccerPlayer(“Terry”, 13.3);players.add(myPlayer);

OR

Page 49: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 50: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Accessing an element

players.get(0);

Page 51: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Accessing an element

players.get(0);

Start with the name of the ArrayList.

Page 52: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Accessing an element

players.get(0);

Use .get (a method call) instead of square brackets like we did with arrays.

Page 53: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Accessing an element

players.get(0);

Put the index of the element you want to use inside parentheses.

Page 54: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists - Changing the value of an element

players.set(2, new SoccerPlayer(“Terry”, 13.3));

Page 55: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 56: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 57: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 58: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists - Removing an element

players.remove(2);

Page 59: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 60: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Loop Access 1

for(int i = 0; i < players.size(); i++) {System.out.println(players.get(i));

}

Page 61: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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

Page 62: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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.

Page 63: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

ArrayLists – Loop Access 2

for (SoccerPlayer p : players) {System.out.println(p);

}

Page 64: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

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”

Page 65: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

SoccerRoster.java with ArrayLists

Page 66: CP122 – CS Ics.coloradocollege.edu/~mwhitehead/courses/2017_2018/CP122/Lectures/5.pdf · CP122 – CS I Arrays and ArrayLists. Tech News! AI company Vicarious develops Recursive

A Robot Invasion!

Let's make 100 Robot objects and add them to the screen!