YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

Thisdocumentiscopyright(C)StanfordComputerScienceandMartyStepp,licensedunderCreativeCommonsAttribution2.5License.Allrightsreserved.BasedonslidescreatedbyKeithSchwarz,MehranSahami,EricRoberts,StuartReges,andothers.

CS106A,Lecture19ArrayLists

suggestedreading:JavaCh.11.8

Page 2: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

2

ArrayListsHashMaps

Wearehere

Page 3: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

3

Learning Goals• KnowhowtostoredatainandretrievedatafromanArrayList.

Page 4: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

4

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 5: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

5

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 6: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

6

Tic-Tac-ToeLet’suse2DarraystocreateaConsoleProgram versionofTic-Tac-Toe.

Page 7: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

7

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 8: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

8

Limitations of Arrays• Sizemustbespecifieduponcreation• Can’tadd/remove/insertelementslater• Nobuilt-inmethodsforsearching,etc.• Can’tprintarrayswithoutArrays.toString (orArrays.deepToString)

index 0 1 2 3 4 5 6 7 8 9

value 12 49 -2 26 5 17 -6 84 72 3

Page 9: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

9

Introducing… ArrayLists!•Avariabletypethatrepresentsalistofitems.•Youaccessindividualitemsbyindex.•Storeasingletypeofobject(String,GRect,etc.)•Resizable– canaddandremoveelements•Hashelpfulmethodsforsearchingforitems

Page 10: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

10

Our First ArrayList

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

Page 11: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

11

Our First ArrayList

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

import java.util.*;

Page 12: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

12

Our First ArrayList

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

Page 13: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

13

Our First ArrayList

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

Page 14: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

14

Our First ArrayList

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

Type of items your ArrayList will store.

Page 15: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

15

Our First ArrayList

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

Page 16: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

16

Our First ArrayList

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

Page 17: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

17

Our First ArrayList

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

Page 18: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

18

Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();

Page 19: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

19

Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();

// Add an element to the backlist.add("Hello"); // now size 1

“Hello”

Page 20: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

20

Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();

// Add an element to the backlist.add("Hello"); // now size 1

list.add("there!"); // now size 2

“Hello”

“Hello” “there!”

Page 21: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

21

Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1

list.add("there!"); // now size 2

// Access elements by index (starting at 0!)println(list.get(0)); // prints "Hello"println(list.get(1)); // prints "there!”println(list); // prints ["Hello", "there!"]

“Hello”

“Hello” “there!”

Page 22: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

22

Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1

list.add("there!"); // now size 2

// Access elements by index (starting at 0!)for (int i = 0; i < list.size(); i++) {println(list.get(i));

}

“Hello”

“Hello” “there!”

Page 23: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

23

Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1

list.add("there!"); // now size 2

// Access elements by index (starting at 0!)for (int i = 0; i < list.size(); i++) {println(list.get(i));

}

“Hello”

“Hello” “there!”

Page 24: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

24

Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1

list.add("there!"); // now size 2

// Access elements in order (also for arrays!)for (String str : list) {println(str);

}

“Hello”

“Hello” “there!”

Page 25: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

25

Iterating Over ArrayLists// Access elements in order (also for arrays!)for (String str : list) {println(str);

}

// equivalent to

for (int i = 0; i < list.size(); i++) {String str = list.get(i);println(str);

}

Page 26: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

26

Iterating Over ArrayLists// Access elements in order (also for arrays!)for (String str : list) {println(str);

}

// equivalent to

for (int i = 0; i < list.size(); i++) {String str = list.get(i);println(str);

}

Page 27: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

27

Bad Times with ArrayLists// Create an (initially empty) listArrayList<String> list = new ArrayList<>();

// Wrong type – bad times! Won’t compileGLabel label = new GLabel("Hello there!");list.add(label);

// Invalid index! IndexOutOfBounds Exceptionprintln(list.get(2));

Page 28: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

28

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 29: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

29

Example: Reversible Writing

IamnotapersonwhocontributesAndIrefusetobelievethat

Iwillbeuseful

Let’s write a program that reverses a text file.

Page 30: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

30

Example: Reversible Writing

IamnotapersonwhocontributesAndIrefusetobelievethat

Iwillbeuseful

IwillbeusefulAndIrefusetobelievethat

Iamnotapersonwhocontributes

Let’s write a program that reverses a text file.

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

Page 31: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

31

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

“Iamnotapersonwhocontributes”

Page 32: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

32

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

“Iamnotapersonwhocontributes”"AndIrefusetobelievethat"

Page 33: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

33

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

“Iamnotapersonwhocontributes”"AndIrefusetobelievethat"

“Iwillbeuseful”

Key idea: fill an ArrayList with each line in the file

Page 34: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

34

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

“Iamnotapersonwhocontributes”"AndIrefusetobelievethat"

“Iwillbeuseful”

Page 35: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

35

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

“Iamnotapersonwhocontributes”"AndIrefusetobelievethat"

“Iwillbeuseful”

Key idea: print the ArrayList items in reverse order

Page 36: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

36

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 37: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

37

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 38: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

38

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 39: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

39

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 40: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

40

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 41: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

41

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 42: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

42

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 43: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

43

Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try {Scanner s = new Scanner(new File(filename));ArrayList<String> lines = new ArrayList<>();

// Read all lines and store in our ArrayListwhile (scanner.hasNextLine()) {

lines.add(scanner.nextLine());}

// Output the lines from back to frontfor (int i = lines.size() - 1; i >= 0; i--) {

println(lines.get(i));}

} catch (IOException ex) {println("Could not read file.");

}

Page 44: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

44

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 45: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

45

ArrayList Methodslist.add(value); appendsvalueatendoflistlist.add(index, value); insertsgivenvaluejustbeforethegivenindex,

shiftingsubsequentvaluestotherightlist.clear(); removesallelementsofthelistlist.get(index) returnsthevalueatgivenindexlist.indexOf(value) returnsfirstindexwheregivenvalueisfoundinlist

(-1ifnotfound)list.isEmpty() returnstrue ifthelistcontainsnoelementslist.remove(index); removes/returnsvalueatgivenindex,shifting

subsequentvaluestotheleftlist.remove(value); removesthefirstoccurrenceofthevalue,ifanylist.set(index, value); replacesvalueatgivenindexwithgivenvaluelist.size() returnsthenumberofelementsinthelistlist.toString() returnsastringrepresentationofthelist

suchas"[3, 42, -7, 15]"

Page 46: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

46

Insert/remove• Ifyouinsert/removeinthefrontormiddleofalist,elementsshift tofit.

list.add(2, 42);• shiftelementsrighttomakeroomforthenewelement

list.remove(1);• shiftelementslefttocoverthespaceleftbytheremovedelement

index 0 1 2 3 4value 3 8 9 7 5

index 0 1 2 3 4 5value 3 8 42 9 7 5

index 0 1 2 3 4 5value 3 8 42 9 7 5

index 0 1 2 3 4value 3 42 9 7 5

Page 47: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

47

Example: Planner

Page 48: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

48

Example: Planner• Let’swriteaprogramtohelpplanoutourday

– Theprogramfirstpromptsforthingsyouwanttodotoday– Then,itaskstheusertore-inputtheminorderofcompletion– Finally,itoutputstheordertheuserhaschosenfortheirtasks

Page 49: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

49

Planner: Approach

“Walk Daisy”

Todos:

Page 50: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

50

Planner: Approach

“Walk Daisy”

“Play Zelda”

Todos:

Page 51: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

51

Planner: Approach

“Walk Daisy”

“Play Zelda”

“Lunch with Avi”

Todos:

Page 52: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

52

Planner: Approach

“Walk Daisy”

“Play Zelda”

“Lunch with Avi”

Todos:

Order: “WalkDaisy”

Page 53: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

53

Planner: Approach

“Play Zelda”

“Lunch with Avi”

Todos:

Order: “WalkDaisy”

Page 54: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

54

Planner: Approach

“Play Zelda”

Todos:

Order: “WalkDaisy”

“Lunch with Avi”

Page 55: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

55

Planner: Approach

Todos:

Order: “WalkDaisy”

“Lunch with Avi”

“Play Zelda”

DONE!

Page 56: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

56

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 57: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

57

ArrayLists + Primitives = 💔// Doesn’t compile LArrayList<int> list = new ArrayList<>();

Unlike arrays, ArrayLists can only store objects!

Page 58: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

58

ArrayLists + Primitives = 💔

Primitive “Wrapper”Classint Integerdouble Doubleboolean Booleanchar Character

Page 59: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

59

ArrayLists + Wrappers = ❤// Use wrapper classes when making an ArrayListArrayList<Integer> list = new ArrayList<>();

// Java converts Integer <-> int automatically!int num = 123;list.add(num);

int first = list.get(0); // 123

Conversion happens automatically!

Page 60: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

60

Array vs. ArrayListArrayList

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

list.add(1); // [1]list.add(2); // [1, 2]

list.set(0, 3); // [3, 2]int x = list.get(0); // 3

list.add(4); // [3, 2, 4]list.contains(2); // true

Array

int[] arr = new int[2]; // [0, 0]

arr[0] = 1; // [1, 0]arr[1] = 2; // [1, 2]

arr[0] = 3; // [3, 2]int x = arr[0]; // 3

[no equivalent]

Page 61: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

61

Array vs. ArrayList

Whydobothoftheseexistinthelanguage?– ArraysareJava'sfundamentaldatastorage– ArrayList isalibrarybuiltontopofanarray

WhenwouldyouchooseanarrayoveranArrayList?– Whenyouneedafixedsizethatyouknowaheadoftime

–Simplersyntaxforgetting/setting–Moreefficient

– Multi-dimensional arrays(e.g.images)– Histograms/tallying

Page 62: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

62

Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayLists vs.arrays•Recap

Page 63: CS 106A, Lecture 19 ArrayLists - web.stanford.edu · This document is copyright (C) ... CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8. 2 ArrayLists HashMaps ...

63

Recap• ArrayLists areavariabletyperepresentingalistofitems• Unlikearrays,ArrayLists have:

– Theabilitytoresizedynamically– Usefulmethodsyoucancallonthem

• UnlikeArrayLists,arrayshave:– Theabilitytostoreanytypeofitem,notjustobjects

NextTime:HashMaps


Related Documents