Search Header Logo
Slides8.1_2DArrays

Slides8.1_2DArrays

Assessment

Presentation

Computers

11th Grade

Practice Problem

Medium

Created by

Lauren Wheelwright

Used 1+ times

FREE Resource

23 Slides • 5 Questions

1

media

2

1D Arrays - Review

String[] arr1 = new String[6];

​0

1​

2​

3​

4​

5​

​null

​null

null​

null​

null​

null​

boolean[] arr2 = new boolean[2];

​i=0

​i=1

​false

​false

3

1D Arrays - Review

String[] arr1 = new String[6];
arr1[2] = "yay!";

​0

1​

2​

3​

4​

5​

​null

​null

"yay!"​

null​

null​

null​

boolean[] arr2 = new boolean[2];
arr2[arr2.length - 1] = true;

​i=0

​i=1

​false

​true

4

2D Arrays

String[][] arr1 = new String[3][5];

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

5

2D Arrays

String[][] arr1 = new String[3][5];

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​# Rows

​{

6

2D Arrays

String[][] arr1 = new String[3][5];

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​# Rows

​{

​# Columns

7

2D Arrays

String[][] arr1 = new String[3][5];

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​null

​# Rows

​{

​# Columns

Type stored

8

Multiple Choice

What is the proper syntax to initialize a 6x6 2D String array?

1

String[] array = new String[6][6];

2

int[][] array = new int[6][6];

3

String[][] array = new String[6];

4

String[][] array = new String[6][6];

9

Initialize arrays with no default values

int[] arr = {5,8,9,4};

​i=0

i=1​

i=2​

i=3​

​5

8​

9​

4​

10

Initialize arrays with no default values

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

int[] arr = {5,8,9,4};

​i=0

i=1​

i=2​

i=3​

​5

8​

9​

4​

​3

4​

5​

​6

7​

8​

​9

0​

1​

11

Initialize arrays with no default values

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

int[] arr = {5,8,9,4};

​i=0

i=1​

i=2​

i=3​

​5

8​

9​

4​

​3

4​

5​

​6

7​

8​

​9

0​

1​

int arr[][] = {
{4,3},
{1,8},
{7,9}
};

​4

3​

​1

8​

​7

9​

​I know this looks made up, but it's legit!

12

Multiple Choice

Which of these would cause a compilation error?

1

        String[][] w = new int[4][3];

2

        boolean[][] y = {{true,false}, {false, true}};

3

        int z[][] = {{3,4}, {5,6}, {7,8}};

4

        int[][] x = new int[0][0];

13

Indexing into a 2D array

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

// Print # rows
System.out.println(_________);

// Print # columns
System.out.println(_________);

type[][] name = new type[rows][cols];

​0

0​

0​

​0

0​

0​

0​

0​

0​

​0

0​

0​

14

Indexing into a 2D array

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

// Print # rows
System.out.println(x.length);

// Print # columns
System.out.println(_________);

type[][] name = new type[rows][cols];

​0

0​

0​

​0

0​

0​

0​

0​

0​

​0

0​

0​

15

Indexing into a 2D array

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

// Print # rows
System.out.println(x.length);

// Print # columns
System.out.println(x[0].length);

type[][] name = new type[rows][cols];

​0

0​

0​

​0

0​

0​

0​

0​

0​

​0

0​

0​

16

Multiple Choice

int[][] grid = new int[5][3]

What is the value of grid.length?

1

0

2

5

3

3

4

15

17

Multiple Choice

int[][] grid = new int[5][3]

What is the value of grid[0].length?

1

0

2

5

3

3

4

15

18

Predict...

int[][] x = {
{9,8,7},
{6,5,4},
{3,2,1},
{0, -1, -2}
};
System.out.println(x[2][1]);

​9

8​

7​

​6

​5

4​

3​

​2

1​

0​

-1​

​-2

19

Predict...

int[][] x = {
{9,8,7},
{6,5,4},
{3,2,1},
{0, -1, -2}
};
System.out.println(x[2][1]);

​9

8​

7​

​6

​5

4​

3​

​2

1​

0​

-1​

​-2

20

Predict...

int[][] x = {
{9,8,7},
{6,5,4},
{3,2,1},
{0, -1, -2}
};
System.out.println(x[2][1]);
// Prints 2

​9

8​

7​

​6

​5

4​

3​

​2

1​

0​

-1​

​-2

21

Multiple Choice

A 2D array is initialized:

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

What is the result of array[0][1] + array[2][0]?

1

10

2

15

3

9

4

5

22

The updateValue method changes the value in arr at [row][col] to value

Call updateValue 3 times

public static void updateValue(int[][] arr, int row, int col, int value)

  • Set last value of first array to the length of the entire 2D array.

  • Set last value of 2nd array to the total number of values in the entire 2D array.

  • Set last value of 3rd array to the sum of the 1st value in 2D array and last value in 2D array.

updateValue(myArray, 1, myArray[0].length-1, 12);

The line above sets the last value of the 2nd array to 12

23

TODO

  • Initialize an 8x8 2D String array

  • Add all chess pieces to the board!

    • chess[0][0] = "Rook"; // sets the top left square to "Rook"

24

Project 8.1 - 8.1.7 TicTacToe

We'll learn this tomorrow!

25

media

26

media

27

media

28

media
media

Show answer

Auto Play

Slide 1 / 28

SLIDE