zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion Bank

What are the differences between rectangular and non-rectangular 2D arrays?

Rectangular: All inner arrays have the same length. Non-rectangular: Inner arrays can have different lengths.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the differences between rectangular and non-rectangular 2D arrays?

Rectangular: All inner arrays have the same length. Non-rectangular: Inner arrays can have different lengths.

What are the differences between using arrayName.length and arrayName[0].length?

arrayName.length: Returns the number of rows. arrayName[0].length: Returns the number of columns (for rectangular arrays).

How are 2D arrays stored in memory?

As a nested array structure, where the outer array contains references to inner arrays.

How do you access an element in a 2D array?

Using double-index notation: arrayName[row_index][column_index].

What are the two ways to initialize 2D arrays?

  1. Initialize an empty 2D array. 2. Initialize a pre-existing 2D array.

Why is graphical representation only suitable for rectangular arrays?

Because it relies on the grid/table/matrix analogy, which requires consistent row lengths.

Explain the concept of rows and columns in a 2D array.

Rows are the horizontal arrays, and columns are the vertical arrays. The first dimension determines the number of rows, and the second determines the number of columns.

What happens if you try to access an element outside the bounds of a 2D array?

An ArrayIndexOutOfBoundsException is thrown.

What is the significance of the first dimension in a 2D array?

It defines the number of rows in the array.

What is the significance of the second dimension in a 2D array?

It defines the number of columns in the array (for rectangular arrays).

How does the concept of 'nested arrays' relate to 2D arrays?

A 2D array is essentially an array where each element is another array, hence 'nested'.

Why is it important to understand the memory representation of 2D arrays?

It helps in understanding how data is organized and accessed, especially when dealing with complex algorithms.

What does the following code output? int[][] arr = {{1, 2}, {3, 4}}; System.out.println(arr[0][1]);

2

Identify the error in the following code: int[][] arr = new int[2][3]; System.out.println(arr[2][1]);

ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 (first dimension).

What does the following code output? int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr.length);

2

What does the following code output? int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr[0].length);

3

Identify the error in the following code: int[][] arr = new int[2][]; arr[0] = {1, 2}; arr[1] = {3, 4, 5};

Missing 'new int[]' when assigning values to arr[0] and arr[1]. Should be: arr[0] = new int[] {1, 2}; arr[1] = new int[] {3, 4, 5};

What does the following code output? int[][] arr = {{1}, {2, 3}, {4, 5, 6}}; System.out.println(arr[2][1]);

5

What does the following code output? int[][] arr = new int[3][4]; System.out.println(arr[1][2]);

0

Identify the error in the following code: int[][] arr = new int[3][4]; arr[1,2] = 5;

Incorrect syntax for accessing 2D array elements. Should be arr[1][2] = 5;

What does the following code output? int[][] arr = {{1, 2}, {3, 4}}; System.out.println(arr[1][0]);

3

What does the following code output? int[][] arr = {{1, 2}, {3, 4}}; System.out.println(arr[0][0]);

1