Presentation is loading. Please wait.

Presentation is loading. Please wait.

2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the.

Similar presentations


Presentation on theme: "2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the."— Presentation transcript:

1 2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the example array, will the following statement work? gradeTable[7][2] = 82 ;

2 public class gradeExample { public static void main( String[] arg ) { // declare and construct a 2D array int[][] gradeTable = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} }; System.out.println("grade 0,0: " + gradeTable[0][0]); System.out.println("grade 2,4: " + gradeTable[2][4]); gradeTable[5][3] = 99 ; int sum = gradeTable[0][1] + gradeTable[0][2] ; System.out.println( "sum: " + sum ); }

3 More Important Stuff: Rows are numbered from 0 to N-1, where N is the number of rows Columns are numbered from 0 to M-1, where M is the number of columns. A 2D array with N rows and M columns per row will have N times M number of cells. To find the number of rows in an arrary: int numRows = gradeTable.length; To find the number of columns in a particular row you must access the row first int numCols = grade.Table[0].length;

4 Printing 2D Arrays To print a 2D array you must loop through the row then the columns or you could loop through a column then the row. for ( int row=0; row < gradeTable.length; row++ ) { System.out.print("Row " + row + ": "); for ( int col=0; col < gradeTable[row].length; col++ ) System.out.print( gradeTable[row][col] + " "); System.out.println(); }

5 Different lengths public class UnevenExample2 { public static void main( String[] arg ) { // declare and construct a 2D array int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; System.out.println("Length is: " + uneven.length ); for(int row =0; row < uneven.length; row++){ System.out.println("Row: " + row + " Column Length is: " + uneven[row].length); } 194 02 01234 To access the column length: uneven[0].length; uneven[1].length; uneven[2].length;

6 Tutorial https://chortle.ccsu.edu/java5/Notes/chap49C/c h49C_1.html https://chortle.ccsu.edu/java5/Notes/chap49C/c h49C_1.html Go through the tutorial on 2D Arrays Observe the programs – GradeExample.java – UnevenExample.java – Take the quiz: http://www.cs.iastate.edu/~honavar/JavaNotes/Notes /chap49C/chap49Cquiz.html http://www.cs.iastate.edu/~honavar/JavaNotes/Notes /chap49C/chap49Cquiz.html

7 2D Arrays: PowerPoint: Java Programs: GradeExample.java UnevenExample2.javaGradeExample.javaUnevenExample2.java https://chortle.ccsu.edu/java5/Notes/chap49C/ch49C_1.htmlhttps://chortle.ccsu.edu/java5/Notes/chap49C/ch49C_1.html Go through the Tutorial on 2D Arrays at Chortle Website. Take the Quiz: http://www.cs.iastate.edu/~honavar/JavaNotes/Notes/chap49C/chap49Cquiz.html http://www.cs.iastate.edu/~honavar/JavaNotes/Notes/chap49C/chap49Cquiz.html 1. ArrayList Practicehttp://practiceit.cs.washington.edu/ http://practiceit.cs.washington.edu/ Do the following: Choose any 6: 3 of the problems cannot be multiple choice. Must type the method. University of Washington CSE 143 (CS2)  ArrayList (9) ArrayList maxLength (id #708)maxLength range (id #709)range minToFront (id #710)minToFront removeEvenLength (id #711)removeEvenLength stutter (id #712)stutter removeShorterStrings (id #713)removeShorterStrings switchPairs (id #714)switchPairs removeDuplicates (id #715)removeDuplicates markLength4 (id #716)markLength4  ArrayIntList (9) ArrayIntList removeAll (id #717)removeAll printInversions (id #718)printInversions maxCount (id #719)maxCount mirror (id #720)mirror stretch (id #721)stretch longestSortedSequence (id #722)longestSortedSequence runningTotal (id #723)runningTotal isPairwiseSorted (id #724)isPairwiseSorted removeFront (id #725)removeFront


Download ppt "2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the."

Similar presentations


Ads by Google