The list of numeric or string values is stored in the Java array. Sometimes, it is required to find out the maximum value in the array of numeric values for programming purposes. This task can be done using a loop or a particular built-in function in Java. Different ways of finding the maximum value in a Java array are shown in this tutorial.
Example 1: Using a Loop
Create a Java file with the following code that finds out the maximum value of the array of numbers using the “for” loop. An array of 10 numeric values is defined in the code. A variable is initialized with the first element of the array and this variable is reset if any larger value is found during the iteration of the loop.
public static void main(String[] args)
{
//Declare an array of numbers
int numArray[] = {3, 76, 23, 11, 89, 25, 92, 6, 10, 53};
//Initialize the maximum variable
int MaxValue = numArray[0];
//Print the array values
System.out.println("Array values are: ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
//Find out the maximum value
for( int val : numArray)
{
if(MaxValue < val) MaxValue = val;
}
//Print the maximum value
System.out.print("\nThe maximum value is " + MaxValue + ".");
}
}
Output:
The following output appears after executing the code. Here, 92 is the maximum value of the array that is printed in the output:
Example 2: Using a User-Defined Function
Create a Java file with the following code that finds out the maximum value of the numeric array using a recursive user-defined function. A numeric array of 10 elements is declared at the beginning of the code and 10 random numbers within 50 are initialized into the array. Next, the recursive function is called with the first index value, the size of the array, and the array as the arguments to find out the maximum value of the array.
public class MaxArrayValue2
{
//Declare the variable that will store the maximum value
public static int maximum;
public static int FindMax(int i, int len, int narr[])
{
//Compare the current index value with the last index - 1
if(i == len - 1)
{
//Return the larger value based on the condition
if (narr[i] > narr[i + 1]) return narr[i];
else return narr[i + 1];
}
//Call the function recursively until the current index reaches to the last index-1
maximum = FindMax( i + 1, len, narr);
//Return the maximum value based on the condition
if (narr[i] > maximum) return narr[i];
else return maximum;
}
public static void main(String[] args)
{
//Declare an array of 10 elements
int numArray[] = new int [10];
//Insert 10 random values into the array
for(int i = 0; i < 10; i++)
{
Random r = new Random();
numArray[i] = r.nextInt(50); ;
}
//Print the array values
System.out.println("Array values are: ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
//Print the maximum value
System.out.print("\nThe maximum value of the array by using function is " + FindMax(0, numArray.length-1, numArray));
}
}
Output:
The following output appears after executing the code. According to the output, 43 is the maximum value of the array based on the generated random numbers:
Example 3: Using the Max() Method
Create a Java file with the following code that finds out the maximum value of the numeric array using the max() method of stream API. A numeric array of 10 elements is declared at the beginning of the code and 10 random numbers within 50 are initialized into the array. Next, the max() method is used to find out the maximum value of the array.
import java.util.Random;
import java.util.Arrays;
public class MaxArrayValue3 {
public static void main(String[] args)
{
//Declare an array of 10 elements
int numArray[] = new int [10];
//Insert 10 random values into the array
for(int i = 0; i < 10; i++)
{
Random r = new Random();
numArray[i] = r.nextInt(50); ;
}
//Print the array values
System.out.println("Array values are: ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
//Find out the maximum value
int MaxVal = Arrays.stream(numArray).max().getAsInt();
//Print the maximum value
System.out.print("\nThe maximum value of the array by using max() method is " + MaxVal);
}
}
Output:
The following output appears after executing the code. According to the output, 44 is the maximum value of the array based on the generated random numbers:
Conclusion
The methods of finding the maximum value of an array using a loop, recursive function, and max() method are shown in this tutorial.