Writing a function that finds the maximum/minimum value of an array is a bit tedious, as follows::
/**
*
* * @param chars
* Maximum value in @returnchar array
*/
private static int maxValue(char[] chars) {
int max = chars[0];
for (int ktr = 0; ktr < chars.length; ktr++) {
if (chars[ktr] > max) {
max = chars[ktr];
}
}
return max;
}
Is there a function already defined?
frameworks algorithm java array
Use Commons.Lang and Collection for finding maximum/minimum values.
import java.util.Arrays;
import java.util.Collections;
import org.apache.commons.lang.ArrayUtils;
public class MinMaxValue {
public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};
List b = Arrays.asList(ArrayUtils.toObject(a));
System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}
Because Arrays.asList() is inherently enclosing an array, there is no memory burden and does not make a copy of the elements in the array.
© 2024 OneMinuteCode. All rights reserved.