Advanced Arrays Flashcards

1
Q

How do you declare a 2D array?

A

two sets of square brackets.
(int[][] matrix = new int [3][3];)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you initialize a 2D array with values?

A

int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a regular array and an array of arrays in Java?

A

A regular array stores elements of the same type in a contiguous block of memory. An array of arrays (e.g., 2D array) stores references to other arrays, which may have different lengths.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you loop through a 2D array?

A

You use nested loops to access the elements:

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an ArrayList and how is it different from an Array?

A

An ArrayList is a resizable array that allows dynamic resizing. Unlike an array, its size can be changed after creation, and it can only store objects (not primitives).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly