Arrays and Nodes
CSE 373 A Sp 25
Announcements
Review: OOP Components
Classes: define the meaning behind “objects”, including state and behavior 🗺️
Interfaces: set a contract for classes, to have established methods 📝
Review: OOP & Abstraction
Clients: Utilize products, expecting them to perform in certain ways. 👩💻
Implementers: Maintain baseline for clients to use safely (“promise”) 👷♀️
Review: OOP & Abstraction
Abstraction: Can use a product without knowing its inner workings. 🙈
Encapsulation: A tool for hiding implementation details away, often by using classes. 🚧
Pre-Class Q1
Dynamic Arrays
💭 How can we implement an ArrayList? Using an array!
* Is size the same as elementData.length?
ArrayList Code
Pre-Class Q2
Pre-Class Q3
Invariants
ArrayList invariant. The i-th element in the list is always at elementData[i].
Which elementData array is also valid for an ArrayList representing [5, 6]?
1
size: 2
elementData:
5 | 6 | | |
index 0 1 2 3
ArrayList
5 | 6 |
List
index 0 1
5 | | | |
index 0 1 2 3
A
5 | | 6 | |
index 0 1 2 3
B
5 | 6 | 7 | 8 |
index 0 1 2 3
C
| 5 | 6 | |
index 0 1 2 3
D
Aside - Invariants
Invariants simply refer to set rules/conditions that should always hold true. (Connects to mathematics)
Informal EX:
Linked Nodes
💭 How can we implement a LinkedList? Using linked nodes!
Nodes contain:
Our list is built upon a reference to the first link in the sequence.
* Recall reference semantics!
Node Class Code
LinkedList.add
In the homework, we studied the LinkedList method add(E element).
Node current = front;
while (current.next ! = null) {
current = current.next;
}
current.next = new Node(element);
What does each part of this code do? What node will current refer to at the end?
What feels slow about this code? Brainstorm a potential fix.
2
Back Pointer - LinkedDeque
ADTs and Collections
Abstract Data Types indicate general expected functionality, often through interfaces. (List)
We implement ADTs often by using classes. (ArrayList)
Collections refer to objects used to represent objects known as elements*
*What is the E type parameter?
Abstract data types
List<E>. Ordered sequence. Implemented by ArrayList and LinkedList.
Set<E>. Unordered collection of unique elements.
Map<K, V>. Distinct keys associated with values.
Stack<E>. Last-in, first-out (LIFO) sequence.
Queue<E>. First-in, first-out (FIFO) sequence.
3
size: 2
elementData:
5 | 7 | | |
size: 2
front
5 | |
7 | / |
index 0 1 2 3
|
7 |
5 |
push
pop
Stack
| 5 | 7 | |
Queue
add
remove
"apples" | 5 |
"bananas" | 7 |
Map
ArrayList
LinkedList
5 | 7 |
List
index 0 1
Okay - So Now What?
The process of identifying common problems and designing custom solutions to them is central to our course!
Let’s try using our ArrayList and LinkedList knowledge in application.
Music Playlist
Imagine you want to use queues to build a music playlist that can add songs, play the next song (removing it from the queue), and more. The LinkedList class can be used to implement the Queue interface, but you want to explore other options.
How can you use resizing arrays to implement the Queue interface?
add(E element).
remove().
4
| 2 | 4 | |
Queue
add
remove
Thanks! See you all next week! ~
Today’s Song:
“Love at First Sight”
Vacations