Advanced Arrays Flashcards
How do you declare a 2D array?
two sets of square brackets.
(int[][] matrix = new int [3][3];)
How can you initialize a 2D array with values?
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
What is the difference between a regular array and an array of arrays in Java?
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 do you loop through a 2D array?
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]);
}
}
What is an ArrayList and how is it different from an Array?
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).