winter 2016 comp-250: introduction to computer...

77
Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January 28, 2016

Upload: others

Post on 17-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Winter 2016COMP-250: Introduction

to Computer ScienceLecture 6, January 28, 2016

Page 2: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java Generics

element next

|_,•|

Page 3: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java Generics

Page 4: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java Generics

Page 5: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

(Doubly) Linked List

Page 6: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

(Doubly) Linked List Node

element next prev

|9,•,•|

Page 7: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

Page 8: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

Page 9: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

Page 10: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

Page 11: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

Page 12: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 13: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 14: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 15: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 16: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 17: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|3

Page 18: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|3

Page 19: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|3

Page 20: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|3

Page 21: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|3

⊠ ⊠

Page 22: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 23: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 24: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|

head tail size

|•,•,4|

node

|•|

Page 25: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

dummy dummy . Head Tail size

|•,•,2|

Page 26: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|

Page 27: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|

Page 28: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|

Page 29: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|

Page 30: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|

Page 31: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|1

Page 32: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|1

Page 33: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|1

Page 34: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|1

Page 35: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

head tail size

|•,•,2|

node

|•|1

⊠ ⊠

Page 36: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Array vs Linked List

Page 37: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Linked Lists as ADT(Abstract Data Type)

Page 38: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Linked List operations

Page 39: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

dummy dummy . Head Tail size

|•,•,2|

Java LinkedList

• implemented as doubly linked list(with dummies)

• Node class is private

Page 40: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java LinkedList

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

|•,•,2|

Page 41: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java LinkedList

}expensive

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

|•,•,2|

Page 42: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Java LinkedList

Time(n) is 𝜴(n2)

|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|

|•,•,2|

Page 43: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notation

Page 44: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 45: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• when new space is needed - doubles the array size.

|•,•,•,•,•,•,•,•|

element

|•|

Page 46: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• smaller array is copied into bigger array.

|•,•,•,•,⊠,⊠,⊠,⊠|

element

|•|

Page 47: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

array size cap.

|•,4,8|

Java ArrayList

• new element added to new array

|•,•,•,•,⊠,⊠,⊠,⊠|

element

|•|

Page 48: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notation

Page 49: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Cost ?

Page 50: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notation

Page 51: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!

Page 52: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!

Cost is amortized constant time!

Page 53: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

|•,•,•,•| |•,•| |•|20

21

22

23

Page 54: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

To build an arrayof 2k elements

you need 2k+1-1 steps!

|•,•,•,•| |•,•| |•|20

21

22

23

Page 55: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,⊠,⊠,⊠,⊠|

Java ArrayList

To build an arrayof 2k elements

you need 2k+1-1 steps!

Cost is amortized constant time!

|•,•,•,•| |•,•| |•|20

21

22

23

Page 56: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 57: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 58: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Cost is constant!

Page 59: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationget(2)

|•|

Page 60: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationget(2)

|•|

Cost is constant!

Page 61: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationget(2)

|•|

Cost is constant!

Page 62: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationget(2)

|•|

Cost is constant!

Page 63: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•|

array size cap.

|•,4,4|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationget(2)

|•|

Cost is constant!

Page 64: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notation

Page 65: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 66: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 67: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

⤻⤻

Page 68: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

⤻⤻⤻

Page 69: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,⊠,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

⤻⤻⤻⤻

Page 70: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 71: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•|

Page 72: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•| 5

Page 73: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•| Cost is linear!5

Page 74: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•| Cost is linear!

Same for Remove!5

Page 75: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

|•,•,•,•,•,•,⊠,⊠|

array size cap.

|•,4,8|

Java ArrayList

• implementation using arrays of growing sizes

• cannot access using a[i] notationelement

|•| Cost is linear!

Same for Remove!5

Page 76: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

LinkedList vs ArrayList

Page 77: Winter 2016 COMP-250: Introduction to Computer Sciencecrypto.cs.mcgill.ca/~crepeau/COMP250/LEC-6sbs.pdf · Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January

Winter 2016COMP-250: Introduction

to Computer ScienceLecture 6, January 28, 2016