On Making Arrays

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

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

How do you create a 1D array?

int[] array = new int[5];

2
New cards

How do you create a 2D array?

int[][] matrix = new int[3][4];

3
New cards

What is the default value of array elements?

0 for numeric arrays, null for objects.

4
New cards

How do you initialize a 1D array with values?

int[] array = {1, 2, 3, 4, 5};

5
New cards

How do you initialize a 2D array with values?

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

6
New cards

How do you fill a 1D array with user input?

for (int i = 0; i < array.length; i++) { array[i] = scanner.nextInt(); }

7
New cards

How do you fill a 2D array with user input?

for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { matrix[i][j] = scanner.nextInt(); }}

8
New cards

How do you iterate through a 1D array to display elements?

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

9
New cards

How do you iterate through a 2D array to display elements?

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

10
New cards

How do you sum all elements in a 1D array?

int sum = 0; for (int num : array) { sum += num; }

11
New cards

How do you sum all elements in a 2D array?

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

12
New cards

How do you get the length of a 1D array?

array.length

13
New cards

How do you get the number of rows in a 2D array?

matrix.length

14
New cards

How do you get the number of columns in a 2D array?

matrix[i].length (for row i).

15
New cards

What is the index range for an array of size 5?

0 to 4.

16
New cards

How do nested loops work for a 2D array?

Outer loop = rows, Inner loop = columns.

17
New cards

What is the default value for uninitialized elements in an array?

0 for numbers, false for booleans, null for objects.

18
New cards

Can arrays hold objects in Java?

Yes.

19
New cards

.length in arrays represents what?

The total number of elements.

20
New cards

What is a ragged array?

A 2D array where rows have different lengths.