Chapter 8 Multidimensional Arrays Flashcards

1
Q

What is the syntax for declaring an array reference variable:

A

dataType[][] refVar;

[][] is the # of dimensions you want

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

Create an arrayand assign its reference to variable

A

refVar = new dataType[10][10];

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

Combine declaration and creation in one statement (row = 10, col = 10):

A

dataType[][] refVar = new dataType[10][10];

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

Declaring variables of two dimensional arrays[10][10] and creating two dimensional arrays syntax

A

int[][] matrix = new int[10][10];
or
int matrix [] [] = new int[10][10];

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

Access an element of a 2D array:

A

matrix[0][0] = 3;

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

Assign each element of the array a random number:

A

for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
matrix[i][j]; = (int)(Math.random() * 1000);

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

Declare, initialize, and create a two dimensional array with 3 rows and 3 columns with values ascending from 1-12.

A

int[][] array = {
{1,2,3}
{4,5,6}
{7,8,9}
{10,11,12}
}
;

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

How would you express a two dimensional array with 3 rows and 4 columns

A

int[][] x = new int[column][row];

ex.
int[][] x = new int[3][4]

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

Array start at array[0].length. Calling beyond the index causes:

A

ArrayIndexOutOfBoundsException

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

Ragged arrays definition

A

Each row in a two dimensional array is itself an array, so each row can have different lengths.

Not a solid block, like an increasing or decreasing triangle

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

Declare and initialize a random array syntax with 5– rows and 4 cols with values {1,2,3,4,5}, {2,3,4,5}, {3,4,5}, {4,5}, [5}.

A

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

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

7 ways of processing arrays

A
  1. Initializing arrays with input values
  2. Printing arrays
  3. Summing all elements
  4. Summing all elements by column
  5. Which has the largest sum
  6. Finding the smallest index of the largest column
  7. Random Shuffling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Initializing arrays with input values:

A

import java.util.Scanner;

Scanner input = new Scanner(System.in);

System.out.println(“Enter “ + matrix.length + “ rows and “ + matrix[0].length + “columns: “);

for (int row = 0; row < matrix.length; row++){
for (int col = 0; col < matrix[row].length; col++){
matrix[row][col] = input.nextInt();
}
}

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

Printing arrays

A

for (int row = 0; row < matrix.length; row++){
for(int col= 0; col < matrix[row].length; col++){
System.out.print(matrix[row][col] + “ “);
}
System.out.println();
}

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

Summing all elements

A

int total = 0;
for (int row =0; row < matrix.length; row++){
for(int col= 0; col < matrix[row].length; col++){
total += matrix[row][col];
}
}

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

Summing elements by column:

A

for (int col = 0; col < matrix[0].length; col++){
int total = 0;
for(int row = 0; row < matrix.length; row++){
total += matrix[row][col];
System.out.println(“Sum for column “ + col + “ is “ + total);
}

17
Q

Random Shuffling

A

for (int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
int i1 = (int)(Math.random() * matrix.length);
int j1 = (int)(Math.random() * matrix[i].length);

// Swap matrix [i][j] with matrix [i1][j1]
int temp = matrix[i][j];
matrix[i][j] = matrix[i1][j1];
matrix[i1][j1] = temp;
}
}

18
Q

A two dimensional array can be used to store a:

A

table

19
Q

A variable for two dimensional arrays can be declared using the syntax:

A

elementType[][] arrayVar =

20
Q

Each element in a two dimensional array is represented using the syntax:

A

array-Var[rowIndex][columnIndex];

21
Q

You can create and initialize a two-dimensional array
using an array initializer withthe syntax: (shorthand notation)

A

elementType[][] arrayVar = {{row values}, . . ., {row values}}.

22
Q

You can use arrays of arrays to form multidimensional arrays. For example, a variable for three-dimensional arrays can be declared as

A

elementType[][][] arrayVar and a three-dimensional array can be created using new elementType[size1][size2] [size3].

23
Q

//create your first 2 dimensional array of
// 5 rows of values holding 5 values each

//access length of the array

// Find out how many elements are in each of those arrays

A

int[][] my2DArray = new int[5][5];

System.out.println(my2DArray.length); //prints 5

System.out.println(my2DArray[0].length); // prints 4

24
Q

//use a nested for loop to print the value of the indeces?

A

for(int row = 0; row < array.length; row++){
for( int col = 0; col < array[row].length; col++){
System.out.print(array[row][col] + “ “);
}
System.out.println();
}

25
Q

//create and initialize a 2D array with a length of 2 and col length 3

//use values 1-12

A

int[][] array = {
{1, 2, 3 },
{4, 5, 6 },
{7, 8, 9 },
{10,11,12}};