Skip to content

Apr 02: Linked Structures

Learning Objectives

After today's class, you should be able to:

  • Explain how items are inserted into an ArrayList vs a LinkedList.
  • Summarize performance trade-offs for ArrayList and LinkedList.
  • Decide whether to use an ArrayList or a LinkedList in a program.

Lesson Outline

HW8-A Debrief [10 min]

  • Solution available in Canvas Files
  • Part B (60 pts) due Monday, 4/8

Act12 [45 min]

2D Arrays [20 min]

  • Review from CS 149 (but in Java)

Your To-Do List

By Friday but ideally before Thursday's class:

2D Array Lesson

Basic usage:

  • Declare a 2D array variable
    int[][] table;
    
  • Create a 2D array with 10 rows, 3 columns
    table = new int[10][3];
    
  • Assign the value in row 1, column 2
    table[1][2] = 159;
    
  • Print the array contents (not just the address)
    System.out.println(Arrays.toString(table));
    

Nested loops (indexed):

for (int row = 0; row < table.length; row++) {
    for (int col = 0; col < table[row].length; col++) {
        System.out.print(table[row][col] + " ");
    }
    System.out.println();
}

Nested loops (enhanced):

for (int[] row : table) {
    for (int value : row) {
        System.out.print(value + " ");
    }
    System.out.println();
}

Sample Problems

int[][] data = {
    {3, 0, 7, 2},
    {0, 4, 1, 10, 5},
    {9, 0, 0}
};
  • Count how many zeros are in data
  • Get the sum of each row of data
  • Get the max of each column of data