Finding the maximum integer value contained in a matrix using Java -
to find maximum integer value in matrix try code of that:
/* * @param ints * @return max value in array of chars */ public static int maxmatrix(int [][] ints) { int max = ints[0][0]; for(int = 0; < ints.length; i++) { for(int j = 0; j < ints.length){ max = inst[i][j]; } } return max; } my questions are:
- why assigning variable next 1 in array?
- what conditions , why?
max = inst[i][j];
should max = math.max(max, ints[i][j]);
and...
for(int j = 0; j < ints.length){
should for(int j = 0; j < ints[i].length; j++){
so...
public static int maxmatrix(int [][] ints) { int max = ints[0][0]; for(int = 0; < ints.length; i++) { for(int j = 0; j < ints[i].length; j++){ max = math.max(max, ints[i][j]); } } return max; } you need math.max. otherwise you're assigning variable next in array
math.max(max, ints[i][j]) equivalent to:
if (max > ints[i][j] { return ints[i][j]; // or inline in loop: max = ints[i][j]; } else { return max; // or inline in loop: max = max; not needed }
Comments
Post a Comment