In the previous article, we have seen Java Program to Find the Average of an Array
In this article we are going to see how we can find the largest element in an array.
Java Program to Find the Largest Number in an Array
Prerequisite:
See below articles to know more about Array, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Let’s see different ways to find largest element in the array.
- By Comparing Array Elements
- By Using Arrays.sort Method
- By Using array list and collections
- By Using Stream API
Method-1: Java Program to Find the Largest Number in an Array By Comparing Array Elements
Approach:
- Take an array with elements in it.
- Print the array elements.
- Create a variable and store the first element of the array in it.
- Compare the variable with the whole array to find and store the largest element.
- Print the largest element.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) { // Creating the array int arr[] = {12, 2, 34, 20, 54, 6}; // Initializing the first element of the array to large int large=arr[0]; // Compares all the element to find out the largest one for(int i:arr) { if(large<i) large=i; } // Prints the array elements System.out.println("The array elements are"+Arrays.toString(arr)); // Prints the largest element System.out.println("The largest element of the array is: "+large); } }
Output: The array elements are[12, 2, 34, 20, 54, 6] The largest element of the array is: 54
Method-2: Java Program to Find the Largest Number in an Array By Using Arrays.sort Method
Approach:
- Take an array with elements in it.
- Print the array elements.
- Use
Arrays.sort
function to sort the array in ascending order. - Print the last element.
Program:
import java.util.Arrays; import java.util.Scanner; public class array{ public static void main(String args[]) { // Creating the array int arr[] = {12, 2, 34, 20, 54, 6}; // Sorts the array in ascending order Arrays.sort(arr); // Prints the array elements System.out.println("The array elements are"+Arrays.toString(arr)); // Prints the largest element System.out.println("The largest element of the array is: "+arr[arr.length-1]); } }
Output: The array elements are[2, 6, 12, 20, 34, 54] The largest element of the array is: 54
Method-3: Java Program to Find the Largest Number in an Array By Using array list and collections
Approach:
- Take an array with elements in it.
- Print the array elements.
- Convert the array elements into a list.
- Use the
Collection.sort
function to sort the list in ascending order. - Print the last element.
Program:
import java.util.*; import java.util.Scanner; public class array{ public static void main(String args[]) { // Creating the array Integer arr[] = {12, 2, 34, 20, 54, 6}; // Converts the array into a list List<Integer> list=Arrays.asList(arr); // Sorts the array in ascending order Collections.sort(list); // Prints the array elements System.out.println("The array elements are"+Arrays.toString(arr)); // Prints the largest element System.out.println("The largest element of the array is: "+list.get(arr.length-1)); } }
Output: The array elements are[2, 6, 12, 20, 34, 54] The largest element of the array is: 54
Method-4: Java Program to Find the Largest Number in an Array By Using Stream API
Approach:
- Take an array with elements in it.
- Print the array elements.
- Pass the array into the stream function
max( )
to find out the largest element. - Print the element.
Program:
import java.util.*; import java.util.Scanner; public class Main { public static void main(String args[]) { // Creating the array int arr[] = {12, 2, 34, 20, 54, 6}; // Using the stream API int large = Arrays.stream(arr).max().getAsInt(); // Prints the array elements System.out.println("The array elements are"+Arrays.toString(arr)); // Prints the largest element System.out.println("The largest element of the array is: "+ large); } }
Output: The array elements are[12, 2, 34, 20, 54, 6] The largest element of the array is: 54
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: