80 likes | 164 Views
Chapter 11 2D Arrays. Asserting Java Rick Mercer. Two-Dimensional Arrays. Data is sometimes conveniently viewed as tabular data ( in rows and columns) Week# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Student# 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1
E N D
Chapter 11 2D Arrays Asserting Java Rick Mercer
Two-Dimensional Arrays • Data is sometimes conveniently viewed as tabular data (in rows and columns) • Week# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 • Student# • 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 • 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 • 2 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 • 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 • 4 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 • 5 1 0 1 0 1 1 1 0 1 1 1 1 1 0 0 • 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 • 7 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 • 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 • 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 • An Attendance Sheet • Code: 1 represents attendance, 0 indicates absence
Declaring Two-Dimensional Arrays • Data like this can be represented by arrays declared with two subscripts • the first subscript represents the number of rows • the second subscript represents the number of columns • General Form this is an array of arrays typeOrClass[][]identifier =newdata-type[rows][cols] typeOrClass: a primitive type a Java class identifier: name of the two-dimensional array rows: the number of rows stored columns: the number of columns
Referencing individual elements in a 2-D array • Reference to an element of a two-dimensional array requires two subscripts (row and column) 2-D-array-name[row ] [column ] • Each subscript must be bracketed individually • [ row, column ] is an error • Valid references: • student[0][0] = 1; • student[9][14] = 0; • An Invalid reference • student[2, 3] = 1;
Nested for loops to access every element in a 2-D array • int[][] twoDarray = new int[4][6]; // 4 x 6 ints • for(int row = 0; row < 4; row++) { • // Initialize one row • for(int col = 0; col < 6; col++) { • // Reference each column in the row • twoDarray[row][col] = row * col; • } • } • // continued on next slide
Display elements row by row • for(int row = 0; row < 4; row++) { • // Display one row • for(int col = 0; col < 6; col++) { • // Display each column of the row • System.out.print(twoDarray[row][col] + " "); • } • System.out.println(); • } Output?
Code Demo • Implement class Matrix that can do matrix addition • int[][] array = { { 1, 2, 3 }, • { 1, 2, 3 } }; • Matrix a = new Matrix(array); • add these methods • Constructor that takes a file name as an argument • intgetNumberRows() • int getNumberColumns() • Matrix add(Matrix)
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * A type to represent a Matrix of integers. * * Methods include: * 1) getNumRows() * 2) getNumColumns() * 3) add(Matrix) * * @author Rick Mercer */ public class Matrix { private int[][] array; private intnRows; private intnCols; // Make a copy of the argument public Matrix(int[][] initialValues) { nRows = initialValues.length; nCols = initialValues[0].length; array = new int[nRows][nCols]; for (int row = 0; row < nRows; row++) { for (int col = 0; col < nCols; col++) { array[row][col] = initialValues[row][col]; } } } public int get(int row, intcol) { return array[row][col]; } Begin with this unit test and beginning of class Matrix (will be on code demos page) • public class MatrixTest { • @Test • public void testGettersAndSetters() { • int[][] a = { { 1, 2, 3 }, { 6, 5, 4 } }; • // Create a new instance of Matrix with a copy of an allocated 2D array. • Matrix mat1 = new Matrix(a); • assertEquals(2, mat1.getNumRows()); • assertEquals(3, mat1.getNumColumns()); • assertEquals(1, mat1.get(0, 0)); • assertEquals(2, mat1.get(0, 1)); • assertEquals(3, mat1.get(0, 2)); • assertEquals(6, mat1.get(1, 0)); • assertEquals(5, mat1.get(1, 1)); • assertEquals(4, mat1.get(1, 2)); • } • @Test • public void testGettersAndSettersWithThisInputFile() { • // First line int 'matrixData' has nRows and nCols • // 3 5 • // 1 2 3 4 5 • // 10 9 8 7 6 • // -11 -12 -13 -14 -15 • Matrix mat1 = new Matrix("matrixData"); • assertEquals(3, mat1.getNumRows()); • assertEquals(5, mat1.getNumColumns()); • assertEquals(1, mat1.get(0, 0)); • assertEquals(2, mat1.get(0, 1)); • assertEquals(3, mat1.get(0, 2)); • assertEquals(10, mat1.get(1, 0)); • assertEquals(9, mat1.get(1, 1)); • assertEquals(8, mat1.get(1, 2)); • assertEquals(-15, mat1.get(2, 4)); • } • @Test • public void testMatrixAddition() { • int[][] a1 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; • Matrix a = new Matrix(a1); • int[][] a2 = { { -2, 2, -2, 2 }, { 4, -4, 4, -4 } }; • Matrix b = new Matrix(a2); • Matrix c = a.add(b); • // Check all elements • assertEquals(-1, c.get(0, 0)); • assertEquals(4, c.get(0, 1)); • assertEquals(1, c.get(0, 2)); • assertEquals(6, c.get(0, 3)); • assertEquals(9, c.get(1, 0)); • assertEquals(2, c.get(1, 1)); • assertEquals(11, c.get(1, 2)); • assertEquals(4, c.get(1, 3)); • }