Maximum value in an array
Maximum value in a byte array
public class Main {
public static byte max(byte[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
byte max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
}
Maximum value in a double-type array.
public class Main {
public static double max(double[] array) {
if (array== null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
double max = array[0];
for (int j = 1; j < array.length; j++) {
if (Double.isNaN(array[j])) {
return Double.NaN;
}
if (array[j] > max) {
max = array[j];
}
}
return max;
}
}
Maximum value in a float-type array.
public class Main {
public static float max(float[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
float max = array[0];
for (int j = 1; j < array.length; j++) {
if (Float.isNaN(array[j])) {
return Float.NaN;
}
if (array[j] > max) {
max = array[j];
}
}
return max;
}
}
Maximum value in a long-value array.
public class Main {
public static long max(long[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
long max = array[0];
for (int j = 1; j < array.length; j++) {
if (array[j] > max) {
max = array[j];
}
}
return max;
}
}
Maximum value in a short-type array.
public class Main {
public static short max(short[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
short max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
}
Maximum value in an int-type array.
public class Main {
public static int max(int[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
int max = array[0];
for (int j = 1; j < array.length; j++) {
if (array[j] > max) {
max = array[j];
}
}
return max;
}
}
Returns the minimum value in a double-type array.
public class Main {
public static double min(double[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns min
double min = array[0];
for (int i = 1; i < array.length; i++) {
if (Double.isNaN(array[i])) {
return Double.NaN;
}
if (array[i] < min) {
min = array[i];
}
}
return min;
}
}
Returns the minimum value in a float-value array.
public class Main {
public static float min(float[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns min
float min = array[0];
for (int i = 1; i < array.length; i++) {
if (Float.isNaN(array[i])) {
return Float.NaN;
}
if (array[i] < min) {
min = array[i];
}
}
return min;
}
}
Returns the minimum value in a short-type array.
public class Main {
public static byte min(byte[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns min
byte min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Array:
- Append item to array
- Append an object to an array.
- Append one array to another
- Clone Array
- Clones two dimensional float array
- Compare two arrays by reference
- Compare Two byte Arrays
- Compare Two boolean Arrays
- Compare Two char Arrays
- Compare Two double Arrays
- Compare Two float Arrays
- Compare Two int Arrays
- Compare Two long Arrays
- Compare two object arrays
- Compare Two short Arrays
- Compare two two-dimensional array
- Concatenate arrays
- Convert array to set
- Convert string array to string
- Convert string array to List
- Convert multi-dimensional array to string
- Convert an array to a Map
- Copy array
- Copy Elements from One Array to Another
- Copy and add an array at the end of the new array
- Extend an array with additional extra space
- Extend to Double the size of an array
- Fill boolean, byte, char,short, int, long, float array
- Get array upperbound
- Get the number of dimensions
- Insert an Element into a Sorted Array
- Insert the specified element at the specified position in the array
- Insert source array in the target array at offset
- Maximum value in an array
- Minimum value in an array.
- Merge two arrays
- Remove duplicate element from array
- Remove the element at the specified position from the specified array.
- Reverses the order of an array(byte, long, int)
- Search an element in an array and return the index and last index
- Binary Search on an Array
- Shift all elements right by one
- Shift all elements left by one
- Shuffle an array
- Sort byte(char,double, float, int) Array
- Sort char array
- Sort int Array
- Sort long Array
- Sort short Array
- Sort string type array in case insensitive order and case sensitive order
- Sort an Array in Descending (Reverse) Order
- Start with one array
- Subarray from array that starts at offset