Class Notes (1,100,000)
CA (620,000)
McGill (30,000)
COMP (700)
COMP 202 (300)
Lecture 27

COMP 202 Lecture 27: COMP 202 – Lecture 27 – ArrayLists


Department
Computer Science (Sci)
Course Code
COMP 202
Professor
Melanie Lyman- Abramovitch
Lecture
27

This preview shows half of the first page. to view the full 3 pages of the document.
COMP 202 – Lecture 27 – ArrayLists
Linked lists
oWe can iterate through all of the elements by accessing the next and previous
elements in the list
oTo access 6th element, call .getNext() 5 times
Linked lists vs arrays
oLinked lists are better for
Not needing to choose length of data structure in advance
Can continue to append new items in chain
If you want to add an item in the middle of the list, no need to copy all
later items in one index; rather ‘slice’ the chain and insert the item directly
oArrays are better for
Accessing at an index
In an array you do not have to start at the beginning and iterate
through next items
Memory
No need to maintain multiple references to next and previous
objects
Dynamic arrays
oAn array that automatically grows in size when it reaches or approaches its
capacity
oUseful when you need to use an array for its easy ‘at index’ access but you are
not sure of how many elements are needed
Arraylists
oA data structure for dynamic arrays
oYou can treat an arraylist as an array whose pre-determined size is not needed
Using library objects
oAn arraylist is an object that someone else wrote that you can use (like a
scanner)
Creating arraylists
oImport java.util.ArrayList
o
Integer, double, character
oCan be thought as ‘wrappers’ for int, double, char that turn primitive types into
reference types
oConversion will happen automatically
oThey are immutable reference types
oThese classes also have static methods/attributes that have been taught already
Un-boxing and boxing
oAutomatic conversion between types
oPutting an int in an integer is boxing
oTaking an int out is unboxing
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version