On Making Arrays

studied byStudied by 0 people
0.0(0)
Get a hint
Hint

How do you create a 1D array?

1 / 19

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

20 Terms

1

How do you create a 1D array?

int[] array = new int[5];

New cards
2

How do you create a 2D array?

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

New cards
3

What is the default value of array elements?

0 for numeric arrays, null for objects.

New cards
4

How do you initialize a 1D array with values?

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

New cards
5

How do you initialize a 2D array with values?

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

New cards
6

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

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

New cards
7

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(); }}

New cards
8

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

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

New cards
9

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(); }

New cards
10

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

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

New cards
11

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]; }}

New cards
12

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

array.length

New cards
13

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

matrix.length

New cards
14

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

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

New cards
15

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

0 to 4.

New cards
16

How do nested loops work for a 2D array?

Outer loop = rows, Inner loop = columns.

New cards
17

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

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

New cards
18

Can arrays hold objects in Java?

Yes.

New cards
19

.length in arrays represents what?

The total number of elements.

New cards
20

What is a ragged array?

A 2D array where rows have different lengths.

New cards

Explore top notes

note Note
studied byStudied by 3 people
... ago
5.0(1)
note Note
studied byStudied by 30 people
... ago
5.0(1)
note Note
studied byStudied by 1 person
... ago
4.0(1)
note Note
studied byStudied by 3 people
... ago
5.0(1)
note Note
studied byStudied by 13 people
... ago
5.0(2)
note Note
studied byStudied by 4 people
... ago
5.0(1)
note Note
studied byStudied by 87 people
... ago
5.0(4)
note Note
studied byStudied by 4950 people
... ago
4.4(20)

Explore top flashcards

flashcards Flashcard (28)
studied byStudied by 4 people
... ago
5.0(1)
flashcards Flashcard (21)
studied byStudied by 16 people
... ago
5.0(1)
flashcards Flashcard (25)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 24 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 64 people
... ago
4.0(1)
flashcards Flashcard (105)
studied byStudied by 5 people
... ago
5.0(1)
flashcards Flashcard (37)
studied byStudied by 3 people
... ago
5.0(1)
flashcards Flashcard (25)
studied byStudied by 130 people
... ago
4.0(1)
robot