arraylists - stanford university · review: 2d arrays arraylists example: reversible writing...

88
ArrayLists CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 18 With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

Upload: others

Post on 06-May-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

ArrayLists

CS106A, Summer 2019Sarai Gould && Laura Cruz-Albrecht

Lecture 18

With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

Page 2: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Announcements

● Assignment 4 due Monday July 29th at 10AM● Blank lecture code on website schedule

2

Page 3: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Learning Goal for Today

Know how to store data in and retrieve data from an

ArrayList

3

Page 4: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

4

Page 5: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

5

Page 6: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Review 2D: Arrays

int[][] matrix = new int[3][4];

6

# rows # columns

Page 7: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Review 2D: Arrays

int[][] matrix = new int[3][4];

7

2

1

000010203

00010203

00010203

0 0 0 0 0

0 0 0 0

0

0

0

1

0

2

0

3

1

2

An array of arrays A unit (ie, a grid/matrix)

Page 8: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Review 2D: Arrays

int[][] matrix = new int[3][4];

matrix[row][col]; // get element

matrix[row][col] = value; // set element

8

Page 9: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Review: 2D Array Dimensions

9

private int numRows(int[][] matrix) {

return matrix.length;

}

private int numCols(int[][] matrix) {

return matrix[0].length;

}

Page 10: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Review: 2D Arrays For Loops

● The canonical way to loop over a 2D array is with a double for loop

type[][] arr = …for (int row = 0; row < numRows(arr); row++) {

for (int col = 0; col < numCols(arr); col++) {

// do something with arr[row][col] ...

}

}

10

Page 11: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

11

Review: images are 2D arrays of pixels.

Page 12: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

GImage Pixel Methods

12

Page 13: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

13

Page 14: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Limitations of Arrays● Size must be specified upon creation● Can’t add/remove/insert elements later (because size is fixed)

14

Page 15: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Limitations of Arrays● Size must be specified upon creation● Can’t add/remove/insert elements later (because size is fixed)

15

1

0

2

1

3

2

myArray

Can I join?

hedgehog icon from Slack

Page 16: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Limitations of Arrays● Size must be specified upon creation● Can’t add/remove/insert elements later (because size is fixed)

16

1

0

2

1

3

2

myArray

Can I join?Sorry

Page 17: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

How can we help ?

17

Page 18: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

18

Page 19: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

19

1

myArrayList

Can I join?

0 1

2 3

2

Page 20: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

20

1

myArrayList

Can I join?Yes!

0 1

2 3

2

Page 21: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

21

1

myArrayList

yay!

0 1

2 3

2

3

3

Page 22: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

22

1

myArrayList

0 1

2 3

2

3

3

ooh can i come too??

Page 23: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Introducing ArrayLists● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)

23

1

myArrayList

0 1

2 3

2

3

3

3

4

Page 24: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

ArrayLists

24

● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)● Homogenous● Can store any Object type● Access individual items by index

1

myArrayList

0 1

2 3

2

3

3

3

4

Page 25: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

25

Page 26: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

26

import java.util.*;

Page 27: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

27

Page 28: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

28

Type of thing your ArrayList will store

Page 29: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

29

Page 30: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

30

Page 31: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

31

Same type here.

Page 32: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<>();______

32

Can optionally leave empty because of

type inference

Page 33: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayList

ArrayList<String> myArrayList = new ArrayList<String>();

33

Page 34: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

34

myArrayList

Page 35: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

// Adds elements to the back

myArrayList.add(“hi”);

35

myArrayList0

“hi”

Page 36: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

36

myArrayList0 1

“hi” “there”

Page 37: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

println(myArrayList.get(0)); // prints “hi”

println(myArrayList.get(1)); // prints “there”

37

myArrayList0 1

“hi” “there”

Page 38: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

println(myArrayList.get(0)); // prints “hi”

println(myArrayList.get(1)); // prints “there”

// Wrong type - bad times! Won’t compile

GLabel label = new GLabel(“hi there”);

myArrayList.add(label);

38

myArrayList0 1

“hi” “there”

Page 39: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Our First ArrayListArrayList<String> myArrayList = new ArrayList<String>(); // initially empty

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

println(myArrayList.get(0)); // prints “hi”

println(myArrayList.get(1)); // prints “there”

// Wrong type - bad times! Won’t compile

GLabel label = new GLabel(“hi there”);

myArrayList.add(label);

// Invalid index – crashes! IndexOutOfBounds Exception

println(myArrayList.get(2));39

myArrayList0 1

“hi” “there”

Page 40: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Looping over ArrayLists

40

ArrayList<String> myArrayList = new ArrayList<String>();

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

for (int i = 0; i < myArrayList.size(); i++) {

String str = myArrayList.get(i);

println(str);

}

// hi

// there

Page 41: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Looping over ArrayLists

41

ArrayList<String> myArrayList = new ArrayList<String>();

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

for (int i = 0; i < myArrayList.size(); i++) {

String str = myArrayList.get(i);

println(str);

}

// A beautiful way to access each element

for (String str : myArrayList) {

println(str);

}

Page 42: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Looping over ArrayLists

42

ArrayList<String> myArrayList = new ArrayList<String>();

// Adds elements to the back

myArrayList.add(“hi”);

myArrayList.add(“there”);

// Access elements by index (starting at 0!)

for (int i = 0; i < myArrayList.size(); i++) {

String str = myArrayList.get(i);

println(str);

}

// A beautiful way to access each element

for (String str : myArrayList) {

println(str);

}

Page 43: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

ArrayList Methods

43

<T> ?This means,

whatever Type your ArrayList stores

Page 44: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

44

Page 45: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

I am not a person who contributes And I refuse to believe that

I will be useful

45

Page 46: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

I am not a person who contributes And I refuse to believe that

I will be useful

I will be usefulAnd I refuse to believe that

I am not a person who contributes

46"I Have a Dream" by Antonia Lee, Sara Fung, Christy Fung, Rachel Lam http://poets.spice.org.hk/index.php?option=com_content&view=article&id=45:my-family&catid=6:reversepoem&Itemid=7

Page 47: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

47

“I am not a person who contributes”

Page 48: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

48

“I am not a person who contributes”“And I refuse to believe that”

Page 49: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

49

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Page 50: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

50

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Key Idea # 1: fill an ArrayList with each line in the file.

Page 51: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

51

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Page 52: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

52

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Page 53: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

53

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Page 54: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible WritingLet’s write a program that reverses a text file.

54

“I am not a person who contributes”“And I refuse to believe that”

“I will be useful”

Key Idea # 2: print the ArrayList items in reverse order.

Page 55: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 55

Page 56: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 56

Page 57: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 57

Page 58: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 58

Page 59: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 59

Page 60: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 60

Page 61: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 61

Page 62: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Reversible Writingtry {

Scanner scanner = new Scanner(new File(FILENAME));

ArrayList<String> lines = new ArrayList<String>();

// Read all lines and store in our ArrayList

while (scanner.hasNextLine()) {

lines.add(scanner.nextLine());

}

// Output the lines from back to front

for (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));

}

scanner.close();

} catch (IOException ex) {

println("Could not read file.");

} 62

Page 63: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

63

Page 64: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

A Note on Insert/Remove● If you insert or remove an element from a list, any elements to

the right of it shift to fit

64

Page 65: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

A Note on Insert/Remove● If you insert or remove an element from a list, any elements to

the right of it shift to fit

65

list.add(2, 42); // add the value 42 before index 2

Page 66: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

A Note on Insert/Remove● If you insert or remove an element from a list, any elements to

the right of it shift to fit

66

list.add(2, 42); // add the value 42 before index 2

list.remove(1); // remove the element at index 1

Page 67: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Example: Trip Planner

67

It’s summer, and you want to travel! Let’s write a program to plan our itinerary.

● Program first prompts the user for all the cities they want to visit● Then, it asks user to re-enter them in the order they’d like to visit them● Finally, outputs the itinerary: the order in which to visit the cities

Page 68: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

68

Order:

Florence

Page 69: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

69

Order:

Florence Singapore

Page 70: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

70

Order:

Florence Singapore Seattle

Page 71: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

71

Order:

Florence Singapore Seattle

Page 72: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

72

Order:

Florence Seattle

Singapore

Page 73: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

73

Order:

Florence Seattle

Singapore

Page 74: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

74

Order:

Florence

Singapore Seattle

Page 75: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

75

Order:

Florence

Singapore Seattle

Page 76: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Trip Planner: Approach

Cities:

76

Order:

Done!

Singapore Seattle Florence

Page 77: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Let’s Code It!

77

Page 78: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

78

Page 79: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

ArrayLists + Primitives

// Doesn’t compile ~ sad times :(

ArrayList<int> myArrayList = new ArrayList<int>();

79

Unlike Arrays, ArrayLists can only store Objects.

Page 80: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Wrapper Classes

80

Page 81: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

ArrayLists + Primitives// Use wrapper classes when making an ArrayList

ArrayList<Integer> numList = new ArrayList<Integer>();

// Java converts Integer <-> int automatically!

numList.add(22);

numList.add(44);

int firstNum = numList.get(0); // 22

int secondNum = numList.get(1); // 44

81

Conversion happens automatically!

Page 82: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

An Example

82

Let’s check out an example

Page 83: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Arrays vs. ArrayListsOperation

Make a new one

Length?

Get element?

Set element?

Loop?

83

Arrays

int arr = new int[5];

arr.length

arr[i]

arr[i] = value

for(int i = 0; i < arr.length; i++)

ArrayLists

ArrayList<String> list = new

ArrayList<String>();

list.size()

list.get(i)

list.set(i, value)

for(String value : list)

Page 84: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Array vs. ArrayListsWhy do both of these exist in the language?● Arrays are Java's fundamental data storage● ArrayList is a library built on top of an array

When would you choose an array over an ArrayList? ● When you need a fixed size that you know ahead of time

○ Simpler syntax for getting/setting, more efficient● Multi-dimensional arrays (e.g. images)● Histograms/tallying

84

Page 85: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

[Extra Practice] Picking Berries

85

Page 86: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

[Extra Practice] Picking Berries

86

Page 87: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

When you don’t know how many are coming to the party

87

1

hedgehogPartyList

0 1

2 3

2

3

3

I love ArrayLists!! I brought all my friends

Page 88: ArrayLists - Stanford University · Review: 2D Arrays ArrayLists Example: Reversible Writing Example: Trip Planner ArrayLists vs. Arrays 13. Limitations of Arrays Size must be specified

Plan for Today

● Review: 2D Arrays● ArrayLists● Example: Reversible Writing● Example: Trip Planner● ArrayLists vs. Arrays

Next Time: HashMaps!

88