How To Find Highest Number In Array Java
Find the Max Number in an Assortment in Coffee
- Observe Maximum Number in an Array Using the Iterative Fashion
- Find Maximum Number in an Array Using
Stream
- Find Maximum Number in an Array Using
Arrays.sort()
An array contains information of a like type. While yous can already read all the elements and perform several operations on them, this article will show you how to find the max value in an array in Java.
Detect Maximum Number in an Array Using the Iterative Manner
This method is the traditional way to find the maximum number from an assortment. Information technology includes an iterator that is used to go through every element in the array. Below, nosotros accept an array of integers, intArray
; first, nosotros create a variable maxNum
and initialize it with the first element of intArray
.
Nosotros create an enhanced for-loop that takes the array and returns every single element in each iteration. Then, we cheque each element with maxNum
that has 24, and once information technology finds a greater number than 24, it replaces 24 with that number in maxNum
. It volition supervene upon the number in maxNum
until it reaches the end of the array; otherwise, it didn't find a bigger number than the existing value in maxNum
.
public class ArrayMax { public static void chief(String[] args) { int[] intArray = {24, two, 0, 34, 12, 110, 2}; int maxNum = intArray[0]; for (int j : intArray) { if (j > maxNum) maxNum = j; } System.out.println("Maximum number = " + maxNum); } }
Output:
Maximum number = 110
Notice Maximum Number in an Array Using Stream
Java viii introduced the Stream API
that provides several useful methods. I of them is the Arrays.stream()
method that takes an assortment and returns a sequential stream. In our instance, we accept an array of the int
type, and when we pass information technology in the stream, it returns an IntStream
.
The IntStream
function comes with a method max()
that helps to find the maximum value in the stream. Information technology returns an OptionalInt
that describes that the stream might take empty int
values too.
At last, every bit we want the maximum number every bit an int
, nosotros'll utilize the optionalInt.getAsInt()
method that returns the result equally an int
type.
import java.util.Arrays; import java.util.OptionalInt; import java.util.stream.IntStream; public class ArrayMax { public static void chief(String[] args) { int[] intArray = {24, two, 0, 34, 12, 11, 2}; IntStream intStream = Arrays.stream(intArray); OptionalInt optionalInt = intStream.max(); int maxAsInt = optionalInt.getAsInt(); System.out.println("Maximum number = " + maxAsInt); } }
Output:
Maximum number = 34
Detect Maximum Number in an Assortment Using Arrays.sort()
The terminal technique in this listing uses the sorting method that organizes the array in ascending social club. To sort the array, nosotros employ the function Arrays.sort()
and laissez passer intArray
as an argument.
To run into how the array will look like after the sort operation, we print information technology. Now, as the array is sorted and the largest number of all is at the left-most position, we become its position using the intArray.length - 1
part, which is at the final position of the array.
import java.util.Arrays; public class ArrayMax { public static void main(String[] args) { int[] intArray = {24, 340, 0, 34, 12, x, 20}; Arrays.sort(intArray); System.out.println("intArray after sorting: " + Arrays.toString(intArray)); int maxNum = intArray[intArray.length - 1]; Arrangement.out.println("Maximum number = " + maxNum); } }
Output:
intArray later sorting: [0, ten, 12, 20, 24, 34, 340] Maximum number = 340
Write for us
DelftStack manufactures are written by software geeks like you. If y'all likewise would similar to contribute to DelftStack past writing paid articles, yous can bank check the write for us page.
Related Article - Coffee Array
How To Find Highest Number In Array Java,
Source: https://www.delftstack.com/howto/java/java-find-max-in-array/
Posted by: hayesfacithe.blogspot.com
0 Response to "How To Find Highest Number In Array Java"
Post a Comment