1 of 21

Arrays and Nodes

CSE 373 A Sp 25

2 of 21

Announcements

  • Complete Self-Introduction Slide!
  • Course Website + Canvas Overview
  • Course policies in development
  • Deques to be released soon

3 of 21

Review: OOP Components

Classes: define the meaning behind “objects”, including state and behavior 🗺️

Interfaces: set a contract for classes, to have established methods 📝

4 of 21

Review: OOP & Abstraction

Clients: Utilize products, expecting them to perform in certain ways. 👩‍💻

Implementers: Maintain baseline for clients to use safely (“promise”) 👷‍♀️

5 of 21

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. 🚧

6 of 21

Pre-Class Q1

7 of 21

Dynamic Arrays

💭 How can we implement an ArrayList? Using an array!

  1. When adding elements, create a new array with capacity = data.length + 1, and copy over the data. 🐢

  • Track size*! Also start with capacity for 10 elements. Increase this when needed. 🏎️💨

* Is size the same as elementData.length?

8 of 21

ArrayList Code

9 of 21

Pre-Class Q2

10 of 21

Pre-Class Q3

11 of 21

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

12 of 21

Aside - Invariants

Invariants simply refer to set rules/conditions that should always hold true. (Connects to mathematics)

Informal EX:

  • For a queue, the next element to remove (oldest element) is always at the front. This is the element we can peek()! 🚶🚶🚶🚶

13 of 21

Linked Nodes

💭 How can we implement a LinkedList? Using linked nodes!

Nodes contain:

  • A value for its element
  • A reference* (link) to the next node 👉
    • If null, we reached the end!

Our list is built upon a reference to the first link in the sequence.

* Recall reference semantics!

14 of 21

Node Class Code

15 of 21

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

16 of 21

Back Pointer - LinkedDeque

17 of 21

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*

  • Many ADTs fit this definition! Like ‘containers’

*What is the E type parameter?

18 of 21

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

19 of 21

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.

20 of 21

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

21 of 21

Thanks! See you all next week! ~

Today’s Song:

“Love at First Sight”

Vacations