privatestaticintindexOf(int[] array, int target, int start, int end) { for (inti= start; i < end; i++) { if (array[i] == target) { return i; } } return -1; }
privatestaticintlastIndexOf(int[] array, int target, int start, int end) { for (inti= end - 1; i >= start; i--) { if (array[i] == target) { return i; } } return -1; }
public static int min(int…)
1 2 3 4 5 6 7 8 9 10
publicstaticintmin(int... array) { checkArgument(array.length > 0); intmin= array[0]; for (inti=1; i < array.length; i++) { if (array[i] < min) { min = array[i]; } } return min; }
public static int max(int…)
1 2 3 4 5 6 7 8 9 10
publicstaticintmax(int... array) { checkArgument(array.length > 0); intmax= array[0]; for (inti=1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; }
public static int constrainToRange(int, int, int)
1 2 3 4
publicstaticintconstrainToRange(int value, int min, int max) { checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max); return Math.min(Math.max(value, min), max); }
// For pre-sizing a builder, just get the right order of magnitude StringBuilderbuilder=newStringBuilder(array.length * 5); builder.append(array[0]); for (inti=1; i < array.length; i++) { builder.append(separator).append(array[i]); } return builder.toString(); }
public static void sortDescending(int[])
1 2 3 4 5 6 7 8 9
/** * Sorts the elements of {@code array} in descending order. * * @since 23.1 */ publicstaticvoidsortDescending(int[] array) { checkNotNull(array); sortDescending(array, 0, array.length); }
public static void sortDescending(int[], int, int)
1 2 3 4 5 6
publicstaticvoidsortDescending(int[] array, int fromIndex, int toIndex) { checkNotNull(array); checkPositionIndexes(fromIndex, toIndex, array.length); Arrays.sort(array, fromIndex, toIndex); reverse(array, fromIndex, toIndex); }