Java Reference
In-Depth Information
System.out.println("The sum of the elements of row "
+ (row + 1) + " = " + sum + ".");
}
} //end sumRows
public static void largestInRows( int [][] matrix)
{
int largest;
//Largest element in each row
for ( int row = 0; row < matrix.length; row++)
{
largest = matrix[row][0]; //assume that the first
//element of the row is
//largest
for ( int col = 1; col < matrix[row].length; col++)
if (largest < matrix[row][col])
largest = matrix[row][col];
System.out.println("The largest element of row "
+ (row + 1) + " = " + largest + ".");
}
} //end largestInRows
}
Example 9-13 shows how the preceding methods are used in a program.
EXAMPLE 9-13
The following program illustrates how (references to) two-dimensional arrays are passed
as parameters to methods:
// This program illustrates how two-dimensional arrays are
// passed as parameters to methods.
public class TwoDimArraysAsParam
//Line 1
{
//Line 2
public static void main(String[] args)
//Line 3
{
//Line 4
int [][] board ={{23,5,6,15,18},
{4,16,24,67,10},
{12,54,23,76,11},
{1,12,34,22,8},
{81,54,32,67,33},
{12,34,76,78,9}};
//Line 5
TwoDimArraysMethods.printMatrix(board);
//Line 6
System.out.println();
//Line 7
TwoDimArraysMethods.sumRows(board);
//Line 8
System.out.println();
//Line 9
Search WWH ::




Custom Search