Presentation is loading. Please wait.

Presentation is loading. Please wait.

Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.

Similar presentations


Presentation on theme: "Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int."— Presentation transcript:

1 Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int i = 1; i <= values.length; i++) { if(i < values.length && values[i] == values[i-1]) { runLength++; } else { if(runLength > maxRunLength) { maxRunStart = runStart; maxRunLength = runLength; } runStart = i; runLength = 1; } } return maxRunStart; }

2 2D Arrays Two-Dimensional Arrays A two-dimensional array is a table/matrix -- with rows and columns -- where each row is an array, and there are several rows in a table. e.g. a two-dimensional array of 3 rows and 4 columns

3 Create 2D Array To create a two-dimensional array, you specify the number of rows (first) and columns (second). int[][] iMat = new int[3][4]; // integer matrix of 3 rows, 4 columns double[][] dMat = new double[5][3]; // double matrix of 5 rows, 3 columns char[][] cMat = new char[3][3]; // char matrix -- 3 rows, 3 columns

4 Direct Initialization To give initial values for the elements in a two- dimensional array, you can of course set up a loop. Another way is to provide the initial values directly in declaration. int[][] iMat = { {12, 48, 69, 7}, {5, 16, 27, 30}, {51, 3, 72, 9} };

5 Access an element in 2D Array Each row is an individual array Column index starts at 0 To access an element in a two- dimensional array, you specify the row index (first) and column index (second). System.out.println(iMat[0][2]); // prints an element at row 0, column 2 (69 in the example above) iMat[2][2] = 5; // assign 5 to row 2, column 1

6 Length 2D array Lengths of a Two-dimensional Array. Arrays can contain different number of column elements at each row. 0 1 2 3 +----+----+----+----+ 0 | 12 | 48 | 69 | 7 | +----+----+----+----+ 1 | 5 | 16 | 27 | | +----+----+----+----+ 2 | 51 | 3 | | +----+----+----+----+ LENGTH OF ROW AND COLUMN The Row length (number of rows in the array) is found by using length. int numRow = mat.length; Array to the side has 3 rows The Column Length is different for each row so you must access the row first then the column length. int columnLength = mat[0].length // equal 4 int columnLength = mat[1].length // equal 3

7 Traversing Over a 2D Array with for loop Then we can utilize those two lengths to traverse over a two-dimensional array -- also by using a nested loop. Two for loops: First specify the row to loop through and then the column. Use Variables row and col to represent the row and column. int row, col; for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++) System.out.print[matrix[row][col] + “ “ ); } System.out.println(); } int[][] iMat = { {12, 48, 69, 7}, {5, 16, 27, 30}, {51, 3, 72, 9} };

8 For Wednesday Finish the 2 worksheets on arrays Look over notes Write the algorithms for the program


Download ppt "Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int."

Similar presentations


Ads by Google