Now Reading: Find minimum value in java array

Loading
svg
Open

Find minimum value in java array

January 1, 20241 min read

Lists of numeric or string values ​​are stored in Java arrays. Sometimes, for programming purposes it is necessary to find the maximum value in an array of numerical values. This task can be done in Java using a loop or any special built-in function. This tutorial shows various ways to find the maximum value in a Java array.

Example 1: Using a loop

Create a Java file with the following code that finds the maximum value of an array of numbers using a “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 if a larger value is found during the iteration of the loop then this variable is reset.

public class MaxArrayValue1 ,
public static void main,string[] logic,
,
,Declare a range of numbers
int numArray[] , ,3, 76, 23, 11, 89, 25, 92, 6, 10, 53,,

,initialize max variable
int MaxValue = numArray[0],

,print array values
System.out.println,“The array values ​​are:”,,
For ,integer i = 0, I < numArray.length; i++, ,
system.out.print,numArray[i] , ,,,
,

,find the maximum value
For, int val : numArray,
,
If,Max value < val, maxvalue = val;
,

,print maximum value
system.out.print,,\nThe maximum value is “ +maxvalue+ ,,,
,
,

Output:

After executing the code the following output appears. 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 the maximum value of a 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 as arguments to find the first index value, size of the array, and maximum value of the array.

import java.util.Random;
public class MaxArrayValue2
,
,Declare the variable that will store the maximum value
public static int max;

public static int FindMax,int i, int len, int narr[],
,
,Compare current index value Last Index – 1
If,i == len – 1,
,
,Return larger value based on condition
If ,Idiot[i] , Idiot[i + 1], return Idiot[i],
Other return Idiot[i + 1],
,
,call Celebration recursively Until reaches current index Last index-1
max = findmax, i + 1Lane, fool,,

,Return maximum value based on condition
If ,Idiot[i] , Maximum, return Idiot[i],
Other return Maximum;

,

public static void main,string[] logic,
,

,declare an array of 10 elements
int numArray[] = new int [10],

,Pour 10 random values ​​in array
For,integer i = 0, I < 10, i++,
,
random r = new random,,,
numArray[i] = r.nextInt,50,, ,
,

,print array values
System.out.println,“The array values ​​are:”,,
For ,integer i = 0, I < numArray.length; i++, ,
system.out.print,numArray[i] , ,,,
,

,print maximum value
system.out.print,,\nThe maximum value of an array using the function “is + findmax,0numArray.length-1number table,,,
,
,

Output:

After executing the code the following output appears. According to the output, the maximum value of the 43 array based on the random numbers generated is:

Example 3: Using the Max() Method

Create a Java file with the following code that finds the maximum value of a numeric array using the max() method of the 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 the maximum value of the array.

,import required modules
import java.util.Random;
import java.util.Arrays;
public class MaxArrayValue3 ,

public static void main,string[] logic,
,

,declare an array of 10 elements
int numArray[] = new int [10],

,Pour 10 random values ​​in array
For,integer i = 0, I < 10, i++,
,
random r = new random,,,
numArray[i] = r.nextInt,50,, ,
,

,print array values
System.out.println,“The array values ​​are:”,,
For ,integer i = 0, I < numArray.length; i++, ,
system.out.print,numArray[i] , ,,,
,

,find the maximum value
int MaxVal = Arrays.stream,numArray,.max,,.getAsInt,,,
,print maximum value
system.out.print,,\nThe maximum value of the array using max() method is “ +maxwell,,
,
,

Output:

After executing the code the following output appears. According to the output, the maximum value of the 44 array based on the random numbers generated is:

conclusion

This tutorial shows methods to find the maximum value of an array using loops, recursive functions and the max() method.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg