Assuming you want to return an array with two values (namely the min and the max), you can build that in one line like return new int[] { min, max }; I would also prefer Integer.min(int, int) and Integer.max(int, int) for finding those values. Like,
static int[] records(int[] score) {
int max = score[0], min = score[0];
for (int i = 1; i < score.length; i++) {
max = Integer.max(max, score[i]);
min = Integer.min(min, score[i]);
}
return new int[] { min, max };
}
And if you really need m and n as well, you can add them back and return them
static int[] records(int[] score) {
int m = 0, n = 0, max = score[0], min = score[0];
for (int i = 1; i < score.length; i++) {
if (score[i] > max) {
m++;
max = score[i];
}
if (score[i] < min) {
n++;
min = score[i];
}
}
return new int[] { n, m, min, max };
}
2
solved java array method to return an array [closed]