AP Computer Science

2D Arrays

This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

2D Arrays

2D arrays

  • Just as it is possible to make a one dimensional list out of a single data type, it is also possible to make a table out of one data type
  • We can extend the arrays you know to have two dimensions with very similar syntax

Array of Arrays

  • A 2D array is an array of arrays

0

1

2

3

4

Array 0

0

0

0

0

0

Array 1

0

0

0

0

0

Array 2

0

0

0

0

0

Declaration

  • Doing so creates a variable that can hold a 2D array of ints
  • As before, we still need to instantiate the array to have a specific size:
  • To declare a two dimensional array, we use two sets of square brackets ([][]):

int[][] table;

table = new int[5][10];

Visualization of 2D arrays

  • The first value is the rows and the second value is the columns

0

1

2

3

4

5

6

7

8

9

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

0

2

0

0

0

0

0

0

0

0

0

0

3

0

0

0

0

0

0

0

0

0

0

4

0

0

0

0

0

0

0

0

0

0

Automatic Initialization

  • For other types, including Strings, each index in the 2D array must be filled explicitly
  • When you create an int, double, char, or boolean 2D array, the 2D array is automatically filled with certain values

Type

Value

int

0

double

0.0

char

'\0'

boolean

false

Explicit Initialization

  • Using code to put data into the matrix
  • RC Cola!! Row first and columns last

int[][] mat = new int[5][10];

int label = 1;

for(int r = 0; r < mat.length; r++ ){

for(int c = 0; c < mat[r].length; c++ ){

mat[r][c] = label;

label++;

}

}

Explicit Initialization

  • The result of that code is:

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

10

1

11

12

13

14

15

16

17

18

19

20

2

21

22

23

24

25

26

27

28

29

30

3

31

32

33

34

35

36

37

38

39

40

4

41

42

43

44

45

46

47

48

49

50

Explicit Initialization

  • We can also explicitly assign values using sets of braces:

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

0

1

2

0

1

2

3

1

4

5

6

2

7

8

9

Accessing Elements

  • To access an element in a 2D array you need to use the row and column.

int[][] mat = {{1, 2},{4, 5},{7, 8}};

System.out.println(mat[0][0]);

System.out.println(mat[1][1]);

System.out.println(mat[2][1]);

Output

1

5

8

Assigning Elements

  • To assign to an element in a 2D array you need to use the row and column.

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

mat[0][1] = 4;

mat[1][0] = 5;

mat[2][1] = 3;

System.out.println(mat[0][1]);

System.out.println(mat[1][0]);

System.out.println(mat[2][1]);

Output

4

5

3

Connection to for-loops

Nested for loops

  • The most important thing to remember is for each iteration through the outer loop the inner loop will run to completion

for(int out = 0; out < 3; out++){

for(int in = 0; in < 3; in++){

System.out.print("!");

}

System.out.println();

}

Output

!!!

!!!

!!!

Nested for loops

  • Please use scratch paper to keep track of the variables

for(int out = 1; out < 3; out++){

for(int in = 1; in < 3; in++){

System.out.println(out + " " + in);

}

System.out.println();

}

Output

1 1

1 2

2 1

2 2

Nested for loops

  • You can make triangle shapes using nested for loops
  • The inner loops depends on the outer loop

for(int out = 1; out <= 3; out++){

for(int in = 1; in <= out; in++){

System.out.print("!");

}

System.out.println();

}

Output

!

!!

!!!

Nested for loops

  • Here is an example of using two loops nested inside the outer loop
  • For each outer iteration the two inners run to completion

for(int a = 0; a <= 2; a++){

for(int b = a; b <= 2; b++){

System.out.print("!");

}

for(int c = a; c >= 0; c--){

System.out.print("#");

}

System.out.println();

}

Output

!!!#

!!##

!###

for loops and 2D arrays

When using length...you use <

You only use <= when you do length-1

  • Here is a 2D array of ints called mat
  • We can use a for loop to go through the array

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

for(int r = 0; r < mat.length; r++){

for(int c = 0; c < mat[r].length; c++){

System.out.print(mat[r][c] + " ");

}

}

Output

1 2 3 4

for loop summing 2D array

  • Here is a 2D array of ints called mat
  • We can use a for loop to sum up those ints

int sum = 0;

int[][] mat = {{8, 5}, {3, 7, 2}};

for(int r = 0; r < mat.length; r++){

for(int c = 0; c < mat[r].length; c++){

sum += mat[r][c];

}

}

System.out.println(sum);

Output

25

for each loop

for each loop

You cannot assign back to the 2D array using a for each loop!

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

for(int[] row : mat){

for(int n : row){

System.out.print(n + " ");

}

}

Output

1 2 3 4

2D Array Examples

Searching Example

  • This example goes through the 2D array and prints the occurrences of 4

int[][] mat = {{8, 3, 2}, {5, 4, 6}};

for(int r = 0; r < mat.length; r++){

for(int c = 0; c < mat[r].length; c++){

if(mat[r][c] == 4){

System.out.println(r + " " + c);

}

}

}

Output

1 1

Counting Occurrences

  • This example goes through and counts the number of 4's located in mat

int[][] mat = {{8, 3, 4},{6, 4},{9, 4}};

int count = 0;

for(int r = 0; r < mat.length; r++){

for(int c = 0; c < mat[r].length; c++){

if(mat[r][c] == 4){

count++;

}

}

}

System.out.println(count);

Output

3

14.2D_arrays.pptx - Google Slides