data structures: bringing it all together...data structures: bringing it all together cs106a, summer...

48
Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

Upload: others

Post on 05-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Data Structures: Bringing It All Together

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

Lecture 20

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

Page 2: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Announcements

● Blank code is available to download from the website.

● Assignment 5: Image Shop was released yesterday.○ Due Aug. 5th at 10am

2

Page 3: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Plan for Today

● Review: Arrays, 2D Arrays, ArrayLists● Putting ArrayLists inside of HashMaps● Example: Ice Cream Anyone?● Example: Picking Berries!

3

Page 4: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Review 2D: Arrays

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

4

# rows # columns

Page 5: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Review 2D: Arrays

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

5

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 6: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Reivew: 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 < arr.length; row++) {

for (int col = 0; col < arr[0].length; col++) {

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

}

}

6

Page 7: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First ArrayList

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

7

Type of thing your ArrayList will store Same type here.

Page 8: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Looping over ArrayLists

8

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 9: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap

HashMap<String, String> firstMap = new HashMap<String, String>();

9

Data Type of “keys” in our HashMap

Data Type of “values” in our HashMap

Repeated types of key->value pairs

Page 10: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Review: HashMap Commands

10

HashMap<String, String> myMap = new HashMap<String, String>();

// Adds key->value pairs

myMap.put(“dog”, “woof”);

myMap.put(“cat”, “meow”);

// Removes key and the associated value

myMap.remove(“dog”, “woof”);

// A beautiful way to access each key

for (String key : myMap.keySet()) {

println(key);

// print the value associated with the key

println(myMap.get(key));

}

Page 11: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Review: What Does This Code Do?

import java.util.*;

HashMap<String, Integer> numLimbs = new HashMap<>();

numLimbs.put(“dog”, 4);

numLimbs.remove(“dog”);

numLimbs.put(“snail”, 1);

numLimbs.put(“snail”, 105);

numLimbs.put(“octopus”, 8);

println(numLimbs.get(“dog”));

println(numLimbs.get(“snail”));

println(numLimbs.get(“octopus”));

11

numLimbs

“snail” 105 “octopus” 8

null1058

key value key value

Page 12: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Learning Goal for Today

Combining many of the Data Structures we’ve learned!

2D Arrays, HashMaps,

ArrayLists, and Graphics

12

Page 13: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Can we Store Even MORE Data?

So, we can associate one piece of data with one other piece of data.

Can we associate multiple pieces of data withmultiple other pieces of data?

13

ALWAYS. ALWAYS MORE.

Page 14: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Of Course We Can!

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

14

Look, an ArrayList of Strings!

Page 15: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

15

Data Type of “keys” in our HashMap

Page 16: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

16

Data Type of “values” in our HashMap

Page 17: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

17

We can leave this blank because of type inference!

Page 18: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

import java.util.*;

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

18

Page 19: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

import java.util.*;

HashMap<String, ArrayList<String>> firstMap = new HashMap<>();

19

Let’s create a HashMap that maps from animal names to animal sounds!

Basically, we’re creating a dictionary where, if we look up an animal’s name, it will tell us which sound it makes!

Page 20: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

import java.util.*;

HashMap<String, ArrayList<String>> animalSounds = new HashMap<>();

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

dogSounds.add(“woof”);

animalSounds.put(“dog”, dogSounds);

20

sounds

“dog” (“woof”)

key value

Page 21: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

import java.util.*;

HashMap<String, ArrayList<String>> animalSounds = new HashMap<>();

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

dogSounds.add(“woof”);

dogSounds.add(“bark”);

animalSounds.put(“dog”, dogSounds);

21

sounds

“dog” (“woof”, “bark”)

key value

Page 22: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

...animalSounds.put(“dog”, dogSounds);

...//(“woof”, “bark”)ArrayList<String> currSounds = animalSounds.get(“dog”);

22

sounds

“dog” (“woof”, “bark”)

key value

Page 23: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Our First HashMap (Again)

...animalSounds.put(“dog”, dogSounds);

...//(“woof”, “bark”)ArrayList<String> currSounds = animalSounds.get(“dog”);

for(String sound : currSounds){

println(sound);

}

23

sounds

“dog” (“woof”, “bark”)

key value

woofbark

Page 24: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

What Happens Here?

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

24

sounds

“dog” (“woof”, “bark”)

key value

“cat” (“meow”, “hiss”)

key value

Page 25: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

25

sounds

“dog” (“woof”, “bark”)

key value

“cat” (“meow”, “hiss”)

key value

“dog”

animal

Page 26: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

26

sounds

“dog” (“woof”, “bark”)

key value

“cat” (“meow”, “hiss”)

key value

“dog”

animal

Page 27: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

27

sounds

“dog” (“woof”, “bark”)

key value

“cat” (“meow”, “hiss”)

key value

“dog”

animal

“woof”

sound

Page 28: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

28

sounds

“dog” (“woof”, “bark”)

key value

woof

“cat” (“meow”, “hiss”)

key value

“dog”

animal

“woof”

sound

Page 29: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

29

sounds

“dog” (“woof”, “bark”)

key value

woof

“cat” (“meow”, “hiss”)

key value

“dog”

animal

“bark”

sound

Page 30: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

30

sounds

“dog” (“woof”, “bark”)

key value

woofbark

“cat” (“meow”, “hiss”)

key value

“dog”

animal

“bark”

sound

Page 31: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

31

sounds

“dog” (“woof”, “bark”)

key value

woofbark

“cat” (“meow”, “hiss”)

key value

“cat”

animal

Page 32: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

32

sounds

“dog” (“woof”, “bark”)

key value

woofbark

“cat” (“meow”, “hiss”)

key value

“cat”

animal

Page 33: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

33

sounds

“dog” (“woof”, “bark”)

key value

woofbark

“cat” (“meow”, “hiss”)

key value

“cat”

animal

“meow”

sound

Page 34: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

34

sounds

“dog” (“woof”, “bark”)

key value

woofbarkmeow

“cat” (“meow”, “hiss”)

key value

“cat”

animal

“meow”

sound

Page 35: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

35

sounds

“dog” (“woof”, “bark”)

key value

woofbarkmeow

“cat” (“meow”, “hiss”)

key value

“cat”

animal

“hiss”

sound

Page 36: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

36

sounds

“dog” (“woof”, “bark”)

key value

woofbarkmeowhiss

“cat” (“meow”, “hiss”)

key value

“cat”

animal

“hiss”

sound

Page 37: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

...animalSounds.put(“cat”, catSounds); // (meow, hiss)

...for(String animal : animalSounds.getKeySet()){

ArrayList<String> sounds = animalSounds.get(animal);

for(String sound : sounds){println(sound);

}}

Our First HashMap (Again)

37

sounds

“dog” (“woof”, “bark”)

key value

woofbarkmeowhiss

“cat” (“meow”, “hiss”)

key value

Page 38: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Time for a Break with Duke!*

38

Want to get ice cream?! 🍨😊🍦

* Okay, we’re actually writing more code...

Page 39: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Finding a Common Time

Let’s write a program that will find a common time that we can all meet for ice cream!

You’ve probably seen a program that does something like this before. Now we’re going to write it!

39

Page 40: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Finding a Common Time

40

Page 41: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Let’s Code It!

41

Page 42: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Code: Our Ice Cream Program

42

Page 43: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Picking Berries

I always like toppings for my ice cream!

Let’s pick some berries!

43

Page 44: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Picking Berries!

44

Page 45: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Let’s Code It!

45

Page 46: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Code: Picking Berries (Part 1)

46

Page 47: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Code: Picking Berries (Part 2)

47

Page 48: Data Structures: Bringing It All Together...Data Structures: Bringing It All Together CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 20 With inspiration from slides

Plan for Today

● Review: Arrays, 2D Arrays, ArrayLists● Putting ArrayLists inside of HashMaps● Example: Ice Cream Anyone?● Example: Picking Berries!

Next Time: Classes48