# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Find Largest Number in Array Java | Array Programs in Java – 12 | In the previous Java program, we have developed programs to merge two sorted arrays in Java. Now in this program, we will find the largest number in array Java? How to find max value in array Java?
Program description:- Write a Java program to find the largest number in the array.
Example:-
Array: {10, 5, -15, 20, -30}
Largest element = 20
Procedure to develop the method to find the largest number in Array Java,
a) Take a one-dimensional array (assume array variable as arr)
b) Declare a variable max
c) Assign first element of the array to largest variable i.e. max = arr[0]
d) Iterate through all elements of the array using the loop
e) Check the ith element in the array is greater than max?
f) If yes, then update max variable i.e. assign the ith element to the max
g) Else, go to the step
h) Repeat (e) to (g) until the end of the array.
i) Return max
Java Method to Find the Largest Number in an Array
// Java method to find largest number in array
public static int largest(int[] array) {
// declare a variable max
int max = 0;
// assign first element to max
max = array[0];
// compare with remaining elements
// loop
for (int i = 1; i < array.length; i++) {
// compare
if (max < array[i]) max = array[i];
}
return max;
}
At the start, we assume that the first element of the array is the largest number and then compare it with the remaining elements. If any element is larger than it, then assume that number as the largest number. At last, we will get the largest number in the given array.
At starting we should not continue with max = 0 because the array may have negative numbers and since 0 is always greater than negative numbers, therefore, we may get a result as the largest number = 0.
Using the above method let us develop the Java program. We will take array input from the end-user, first, we will ask to enter the size of the array and then enter the array elements. To take input from the end-user we will use the Scanner class, but you can also use the BufferedReader class. After finding the largest number in the given array we will display the result.
Java Program to Find the Largest or Max Number in Array
import java.util.Scanner;
public class ArrayProgram {
// Java method to find largest number in array
public static int largest(int[] array) {
// declare a variable max
// assign first element to max
int max = array[0];
// compare with remaining elements
// loop
for (int i = 1; i < array.length; i++) {
if (max < array[i])
max = array[i];
}
return max;
}
// main method
public static void main(String[] args) {
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// declare variables
int size = 0;
int arr[] = null;
// take length of the array
System.out.print("Enter length of the array: ");
size = scan.nextInt();
// create array
arr = new int[size];
// take array inputs
System.out.println("Enter array elements: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
// method call
System.out.println("Largest element = " + largest(arr));
// close Scanner
scan.close();
}
}
Output for the different test-cases:-
Enter length of the array: 5
Enter array elements:
10 5 -15 20 -30
Largest element = 20
Enter length of the array: 3
Enter array elements:
45 54 30
Largest element = 54
Find the Largest Number in an Array Using Stream
The IntStream contains the max() method which returns OptionalInt.
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] numbers = { 10, 5, -15, 20, -30 };
int largest = Arrays.stream(numbers).max().getAsInt();
System.out.println(largest); // 20
}
}
If the numbers are stored in a list instead of the array then:-
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> numbers = List.of(10, 5, -15, 20, -30);
Integer largest = numbers.stream()
.max((n1, n2) -> Integer.compare(n1, n2))
.get();
System.out.println(largest); // 20
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!