12
1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

Embed Size (px)

Citation preview

Page 1: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

1

BUILDING JAVA PROGRAMSCHAPTER 7.5MULTIDIMENSIONAL ARRAYS

Page 2: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

2

TWO-DIMENSIONAL ARRAY

Allow for the creation of table-like data structures with a row and column format. The first subscript is a row index in a table while the second subscript is a column index in a table.

Declaration

type[][] name = new type [row size][col size]

Examples

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

       

       

       

rows

cols0 1 2 3

012

[0][0] [0][1] [0][2] [0][3][1][0]

[2][0][1][1] [1][2] [1][3][2][1] [2][2] [2][3]

Page 3: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

3

DECLARATIONS

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

int[][] table = { {0,0,0,0},{0,0,0,0},{0,0,0,0} };

Think of a 2D Array as

An Array of Arrays

Page 4: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

4

REFERENCING ELEMENTS OF 2D ARRAY

 2 4  6  8 10 

 3  6 9   12 15 

 4  8 12   16 20 

The array below is a 3 x 5 array of ints

Declaration:Int [][] grid = new int[3][5];

grid[0][0]=

grid[2][2]=

grid[1][3]=

grid[3][0]=

Page 5: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

5

REFERENCING ELEMENTS OF 2D ARRAY

A B C D E

V W X Y Z

H I J K L

The array below is a 3 x 5 array of ints

Declaration:char [][] grid = new char[3][5];

grid[0][0]=

grid[2][2]=

grid[1][3]=

grid[0][5]=

Page 6: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

6

REFERENCING ELEMENTS OF 2D ARRAY

int[][] grid = new int[3][5];for (int row = 0; row < 3; row++){

for (int col = 0; col < 5; col++){grid[row][col] = row + col;

}}

Page 7: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

7

PASSING TWO-DIMENSIONAL ARRAYS TO METHODS

public void printTable (int[][] table){for (int row = 0; row < table.length; row++){

for (int col = 0; col < table[row].length; col++){System.out.printf(“%4d”, table[row][col]);}System.out.println();

}}

Page 8: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

8

LETS TRY SOME CODE

Write a piece of code that constructs a two-dimensional array of integers named table with 5 rows and 10 columns. Fill the array with a multiplication table, so that array element [i][j] contains the value i * j. Use nested for loops to build the array.

Page 9: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

9

MULTIPLICATION TABLEint[][] table = new int[5][10];//declaration

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 10; j++) {

table[i][j] = i * j;

}

}

int[][] table = new int[5][10];//declaration

for (int i = 0; i < table.length; i++) {

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

table[i][j] = i * j;

}

}

Page 10: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

10

MORE CODE MATRIX ADD

Write a method named matrixAdd that accepts a pair of two-dimensional arrays of integers as parameters, treats the arrays as 2D matrices and adds them, returning the result. The sum of two matrices A and B is a matrix C where for

every row i and column j, Cij = Aij + Bij. You

may assume that the arrays passed as parameters have the same dimensions.

Page 11: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

11

public static int[][] matrixAdd(int[][]a, int[][]b){

int rows = a.length;

int cols = 0;

if (rows>0){

cols=a[0].length; }

int[][]sum= new int[rows][cols]; //declare sum matrix

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

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

}

}

return sum;

}

Page 12: 1 BUILDING JAVA PROGRAMS CHAPTER 7.5 MULTIDIMENSIONAL ARRAYS

12

WHAT WE COVERED

•Create, populate

and traverse multidimensional arrays!