Java Programming: Two-Dimensional Array ©https://blogs.ashrithgn.com/ Objectives • To describe how a Two-dimensional array is implemented as an array of arrays. • To understand the structure of Two-dimensional array • To demonstrate processing Two-Dimensional array with sample code Faculty of Computer Science ©https://blogs.ashrithgn.com/ Two-Dimensional Array • payScaleTable Declare the pay scale table as: double [ ][ ] payScaleTable; Or double payScaleTable [ ][ ]; • Create the array as: payScaleTable = new double [4][5]; • A table organized in rows and columns is a very effective means for communicating many different types of information. • In Java, we represent tables as twodimensional arrays. Faculty of Computer Science ©https://blogs.ashrithgn.com/ 2-D Array Implementation Faculty of Computer Science ©https://blogs.ashrithgn.com/ Understanding Length of payScaleTable • Expression: • payScaleTable.length Faculty of Computer Science ©https://blogs.ashrithgn.com/ Cont’d • Expression: payScaleTable[1].length Faculty of Computer Science ©https://blogs.ashrithgn.com/ Accessing an Element Faculty of Computer Science ©https://blogs.ashrithgn.com/ Two-Dimensional Array Processing: Sample Code public class Ch10PayScaleTable { A programmer with skill grade level 2, step 1 earns $36.50 1hr. public static void main (String[] args) { double[ ][ ] payScaleTable = { {10.50, 12.00, 14.50, 16.75, 18.00}, {20.50, 22.25, 24.00, 26.25, 28.00}, This code finds the average pay of the grade 2 programmers. {34.00, 36.50, 38.00, 40.35, 43.00}, {50.00, 60.00, 70.00, 80.00, 99.99} }; double sum = 0.0, average; for(int j = 0; j < 5; j++) { sum += payScaleTable[2][j]; } average = sum / 5; System.out.println("Average of Level 2 Employees: " + average ); System.out.println("\n"); Faculty of Computer Science ©https://blogs.ashrithgn.com/ Cont’d double difference; Display the pay difference at each grade level for(int i = 0; i < 4; i++) { difference = payScaleTable[i][4] - payScaleTable[i][0]; System.out.println("Pay difference at Grade Level "+ i + " is " + difference); } A programmer with skill grade level 2, step 1 earns $36.50 1hr. System.out.println("\n"); for(int i = 0; i < payScaleTable.length; i++) { for(int j = 0; j < payScaleTable[i].length; j++) { System.out.print( payScaleTable [i] [j] + " " ); } System.out.println(""); Display the pay scale table } System.out.println("\n"); Faculty of Computer Science ©https://blogs.ashrithgn.com/ Cont’d for (int i = 0; i < payScaleTable.length; i++) { for (int j = 0; j < payScaleTable[i].length; j++) { payScaleTable[i][j] += 1.50; System.out.print(payScaleTable[i][j] + " " ); } System.out.println(""); } } } Faculty of Computer Science ©https://blogs.ashrithgn.com/ Summary • This lesson describes how two-dimensional arrays are used in Java. • In Java, we represent tables as two-dimensional arrays. • Two indices (one for the row and another for the column) are used to refer to an array element in two-dimensional array. Faculty of Computer Science ©https://blogs.ashrithgn.com/ Let’s Do Exercises!!! ©https://blogs.ashrithgn.com/