🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Arrays class, and the Stream API (Java 8 and later).Table of Contents
- Introduction
- Using Loops
- Using
ArraysClass - Using
StreamAPI - Conclusion
Introduction
In Java, arrays are fixed-size data structures that store elements of the same type. Finding the largest number in an array involves comparing each element to determine the maximum value. This can be done using various methods, each suited to different scenarios.
Using Loops
One way to find the largest number in an array is by iterating through the array using a loop.
Example
public class FindLargestExample {
public static void main(String[] args) {
int[] array = {10, 20, 5, 30, 15};
int largest = findLargest(array);
System.out.println("The largest number in the array is: " + largest);
}
public static int findLargest(int[] array) {
int largest = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
return largest;
}
}
Explanation
- The first element of the array is assumed to be the largest.
- A loop is used to iterate through the array starting from the second element.
- If an element is larger than the current largest, it becomes the new largest.
- The largest number is returned at the end of the loop.
Output:
The largest number in the array is: 30
Using Arrays Class
The Arrays class provides utility methods for arrays, including a method to sort the array.
Example
import java.util.Arrays;
public class FindLargestExample {
public static void main(String[] args) {
int[] array = {10, 20, 5, 30, 15};
int largest = findLargest(array);
System.out.println("The largest number in the array is: " + largest);
}
public static int findLargest(int[] array) {
Arrays.sort(array);
return array[array.length - 1];
}
}
Explanation
- The
Arrays.sortmethod is used to sort the array in ascending order. - The last element of the sorted array is the largest.
- The largest number is returned.
Output:
The largest number in the array is: 30
Using Stream API
The Stream API (introduced in Java 8) provides a modern and concise way to find the largest number in an array.
Example
import java.util.Arrays;
public class FindLargestExample {
public static void main(String[] args) {
int[] array = {10, 20, 5, 30, 15};
int largest = findLargest(array);
System.out.println("The largest number in the array is: " + largest);
}
public static int findLargest(int[] array) {
return Arrays.stream(array)
.max()
.getAsInt();
}
}
Explanation
- A stream is created from the array using
Arrays.stream. - The
maxmethod finds the largest element in the stream. - The largest number is returned using
getAsInt.
Output:
The largest number in the array is: 30
Conclusion
Finding the largest number in an array in Java can be accomplished using various methods, each with its own advantages. Using loops provides a clear and straightforward approach, suitable for any type of array. The Arrays class offers a convenient way to sort the array and find the largest element. The Stream API provides a modern and functional programming approach, making the code more readable and expressive. Depending on your specific use case and preferences, you can choose the method that best fits your needs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment