Two-Dimensional Arrays in Java

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

flashcard set

Earn XP

Description and Tags

20 question-and-answer flashcards reviewing key concepts about two-dimensional arrays in Java.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

What is the simplest form of a multidimensional array in Java that is essentially an array of one-dimensional arrays?

A two-dimensional array, also called a matrix or table.

2
New cards

How do you declare a two-dimensional integer array named matriz in Java?

int[][] matriz;

3
New cards

In the statement "int[][] matriz = new int[3][4];", what do the numbers 3 and 4 represent?

3 represents the number of rows and 4 represents the number of columns, so the matrix size is 3×4.

4
New cards

Which Java keyword is required to allocate memory for an array after it is declared?

new

5
New cards

Write the Java code to create and initialize a 3×3 matrix containing the numbers 1 to 9 in order.

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

6
New cards

Which property provides the total number of rows in a two-dimensional array named mat?

mat.length

7
New cards

Which expression gives the total number of columns in the first row of a two-dimensional array mat?

mat[0].length

8
New cards

How do you access the element located at row index f and column index c in array a?

a[f][c]

9
New cards

What runtime exception occurs if you try to access a matrix element with an invalid index?

ArrayIndexOutOfBoundsException

10
New cards

Describe the standard nested for loop structure for traversing all elements row by row in a matrix.

Use an outer for loop over the rows and an inner for loop over the columns within each row.

11
New cards

How would you modify the traversal to visit the matrix column by column instead?

Make the outer loop iterate over columns and the inner loop iterate over rows.

12
New cards

How does an enhanced for-each loop traverse a two-dimensional array?

The outer for-each iterates through each row array, and the inner for-each iterates through every value in that row.

13
New cards

In the declaration "int a[][], b;", which variables are two-dimensional arrays and which are simple ints?

"a" is a two-dimensional int array; "b" is a simple int variable.

14
New cards

In the declaration "int[][] c, d;", what kinds of variables are c and d?

Both c and d are two-dimensional int arrays.

15
New cards

For the 4×4 magic square shown in the lecture, what is the magic constant (the common sum)?

34

16
New cards

What does the dimension notation m × n signify for a matrix?

A matrix with m rows and n columns.

17
New cards

What is the main difference between storing related data in a matrix versus using parallel arrays?

A matrix stores related items together in one 2-D structure, while parallel arrays split related fields into separate 1-D arrays that must be kept synchronized.

18
New cards

Provide the Java statement that stores the value 33 in row 0, column 1 of int[][] matriz.

matriz[0][1] = 33;

19
New cards

When iterating over rows with index f, what column index should you use to print the main diagonal element?

Use the same index as the row: matriz[f][f].

20
New cards

Which part of a two-dimensional array declaration (e.g., int[][]) indicates that the variable has two dimensions?

The two pairs of square brackets [][] following the data type.