Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass as a parameter and initalize
Some common two dimensional arrays or tables: Chessboard, train tables, spreadsheets int [ ] [ ] sales = new int [4] [7]; – sales is a new two dimensional array. Sales has 4 rows and 7 columns (Java is row major language, rows always first) – sales may be used by four stores (rows) – seven days (columns) a week. ( rows numbered 0..2, columns 0..6) All Java arrays start with zero.
Diagram of the ‘store’ array page 262 – see page 262 for the completely 2filled in array
int [ ] [ ] sales = new int [4] [7]; double temps = new double [10] [24]; Again rows first 4 for sales 10 for temps columns after 7 for sales, 24 for temps. – sales [2] [4] = Integer.parseInt (textField.getText( )); – chessBoard [3] [4] = textField.getText ( ); –Above: how individual elements may be loaded
sales [3] [2] = 99; chessBoard [2] [7] = “Knight” ; // place Knight on a square. –A poor way to sum a two dimensional array – sum = sales[0] [0] + sales [0] [1] + … –// plus all the rest of the elements of the two dimensional array sales Better way: use loops!
Using loops to sum every element of sales two dimensional array int [ ] [ ] sales = new int [4] [7]; int sum; sum = 0; for (int shop = 0; shop < = 3; shop++) { – for ( int dayNumber = 0; dayNumber < = 6; dayNumber++) { sum = sum + sales[shop] [dayNumber] ; } –} // shorter and more powerful
The size of an array double [ ] [ ] info = new double [20] [40]; Int maxsize = info.length; // remember.length from one dimensional array chapter
Passing arrays as parameter one or multi dimensional, all passed the same way private int sum ( int [ ] [ ] array ) { // text of sum } call to method: – int [ ] [ ] sales = new [24] [12] ; int total; – total = sum( sales); the array ‘sales’ is passed to the method sum. The array name ‘sales’ hold the address of the zeroth element, the beginning of the array.
constants private int [ ] [ ] sales = new [7] [7]; Both row and columns are 7 What happens if an additional shop is added to the program? final int day = 7; Final shops = 7; –Easily changed…
Using ‘final’ int final shop = 8; int final days = 7; int [ ] [ ] sales = new int [shops] [days] ; If shops or days rewritten the rest of the code in the program that access those variables and will work with the additional shop for example.
Initializing a two dimensional array int table = new int [10] [10]; sets up an array named table in memory, zeros out every element Arrays can be initialized explicitly – for ( int row = 0; row <.9; row++) { for (int column = 0; column <=9; column++) { table [row] [column] = 99 ; }}
More explicit loading of a two dimensional array int [ ] [ ] array = { { 1, 0,1 }, { 0, 1, 0}}; // loads ‘array’ with in the zeroth row And in the 1 st row
Program ‘Rainfall’ pages 267 screenshot A two dimensional array of rainfall over seven days at three locations. User may change values by entering location [index] day[index] and a new value of amount of rainfall as the data in the element The array is declared and initaliaed – private int [ ] [ ] rainData = {{ 10, 7, 3 …see page 267…}}
Methods used to display, change values and calculate the total rainfall The user must limit row (location) from 0..2 Must limit columns (days) from 0..6 Any integer value for the actual rainfall