Lecture 5: DLLists, Arrays

9/4/2020

Summary of SLLists So Far

One Downside of SLLists

Improvement #7: Fast addLast

Why a Last Pointer Isn't Enough

.last is not enough

Improvement #7: .last and ???

Doubly Linked Lists (Naive)

Doubly Linked Lists (Double Sentinel)

Doubly Linked Lists (Circular Sentinel)

Improvement #8: Fancier Sentinel Nodes

Generic Lists

Integer Only Lists

public class SLList<LochNess> { private class StuffNode { public LochNess item; public StuffNode next; public StuffNode(LochNess i, StuffNode n) { item = i; next = n; } } private StuffNode first; private int size; ... ... ... } public class SLListLauncher { public static void main(String[] args) { SLList<String> s1 = new SLList<>("bone"); s1.addFirst("thugs"); } }

SLists

Generics

Array Overview

Getting Memory Boxes

Arrays

Arrays

Array Basics:

int[] z = null; int[] x, y; x = new int[]{1, 2, 3, 4, 5}; y = x; x = new int[]{-1, 2, 5, 4, 99}; y = new int[3]; z = new int[0]; int xL = x.length; String[] s = new String[6]; s[4] = "ketchup"; s[x[3] - x[1]] = "muffins"; int[] b = {9, 10, 11}; System.arraycopy(b, 0, x, 3, 2);

Array Copy

2D Arrays

Arrays of Array Addresses

int[][] pascalsTriangle; pascalsTriangle = new int[4][]; int[] rowZero = pascalsTriangle[0]; pascalsTriangle[0] = new int[]{1}; pascalsTriangle[1] = new int[]{1, 1}; pascalsTriangle[2] = new int[]{1, 2, 1}; pascalsTriangle[3] = new int[]{1, 3, 3, 1}; int[] rowTwo = pascalsTriangle[2]; rowTwo[1] = -5; int[][] matrix; matrix = new int[4][]; matrix = new int[4][4]; int[][] pascalAgain = new int[][]{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}};

Arrays vs. Classes

Arrays vs. Classes

Another view

int k = x[indexOfInterest]; double m = p.fieldOfInterest; // Won't work double z = p[fieldOfInterest]; // Won't work // No (sane) way to use field of interest double w = p.mass; // Works fine