2 dimensional arrays Steven Wood ©2005
Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet
3D example Can have more than 2 dimensions!
How to think of a 2d array A 2-D array is an array whose components are themselves arrays
Creating an 2d array A two-dimensional array is an array of arrays. In this example: The first index is the 12 months, indexed from 0 to 11. Each month array is an array of 31 days, indexed from 0 to 30 double rainfall[][] = new double[12][31]; Month index Day index
Using initializer lists int myTable[][] = { {23, 45, 65, 34, 21, 67, 78}, {46, 14, 18, 46, 98, 63, 88}, {98, 81, 64, 90, 21, 14, 23}, {54, 43, 55, 76, 22, 43, 33} };
Setting each value individually int myTable[][]; myTable[0,0] = 23; myTable[0,1] = 45; … myTable[3,5] = 43; myTable[3,6] = 33;
Example Track daily temperature for 4 weeks double[][] weektemp = new double[4][7];
Finding # of rows (weeks) to determine the number of rows in a 2D array, use length int numWeeks = weektemp.length; //4
Finding # of columns (days) to determine the number of columns in a 2D array, find length of first row int numDays = weektemp[0].length; // 7 Side note: a 2d array does not have to be rectangular, but we won’t look at this situation
Our example Create a computized mark book for a teacher Assume only 3 students and 3 marks for each Assume all marks have same weighting (e.g. all percents) MarkBook - String theClass[][] - String name +MarkBook(String name, int students, int marks) +initClass() +displayClass()
A model of the information Thomas Robert Katie Store student average in these Class average Name + 3 marks + Average 5 columns blank +3 students+ average 5 columns
Sample runs…
Do this #1… Download the program MarkBook.java Find the constructor Find the line that declares & allocates space for the array It needs to be completed How big should the array be (look at the previous slide) Make sure that you use variables to set the size!
Do this #2… Find the initClass() method Use the picture of the array contents from 2 slides previous Remove the comment marks and complete the assignment statements that fill the array with values. E.g. //theClass[2][0] = theClass[2][0] = “Robert”; Etc.
Example of using a loops int[][] array = new int[10][5]; // print array in rectangular form for (int r=0; r<array.length; r++) { //# of rows // print columns for each row for (int c=0; c<array[r].length; c++) { System.out.print(" " + array[r][c]); } System.out.println(""); }
Do this #3… In MarkBook.java find the displayClass method Use the information on the last slide to complete the method so that it displays the contents of the array Update the main method so that you use the displayClass method to print out the array!
Finding a student’s average Which student? (Which row?) set a variable Add up all the marks (total) All the columns (use a loop!) Hint: the inside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “1, 4”
Do this #4… Complete the method that will find the average for a student (to 1 decimal) Place the average in the proper spot in the array Make sure you update the main method to use this method!
Finding a test average Which test? (Which column?) set a variable Add up all the marks (total) All the rows (use a loop!) Hint: the outside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “4, 1”
Do this #5… Complete the method that will find the average for a test (to 1 decimal) Place the average in the proper spot in the array Make sure that you update the main method to use this method
Do this #6… Add a method that will find the class average It may depend on the student averages being calculated already Place it in the array
Do this #7… Improve the initClass method so that the values can be entered in from the keyboard Maybe the entire class doesn’t have to be entered at one time more realistic!
Do this #8… Add a method that will allow you to change/update a student’s information You maybe should then recalculate the averages (test, student, class) Add a method to delete a student Add a method to delete a test
Sources Text