In this article, we will learn how to find the largest and smallest number in an array in Java. Finding the largest and smallest values is a common task in programming. We will explain this in simple steps to make it easy to understand.
What is an Array in Java?
An array is a collection of elements, like numbers or words, all of the same type. For example, if we have an array of numbers, it could look like this.
int[] numbers = {12, 35, 1, 10, 34, 1};
This array contains six numbers.
How to Approach the Problem?
To find the largest and smallest numbers.
- We start by assuming that the first number in the array is both the largest and the smallest.
- Then, we go through the rest of the numbers in the array, one by one, and compare each number with the current largest and smallest numbers.
- If we find a bigger number, we update the largest. If we find a smaller number, we update the smallest.
Set Up Variables for the Largest and Smallest Numbers
We will use two variables.
- largest: to keep track of the biggest number found.
- smallest: to keep track of the smallest number found.
We will start by setting both largest and smallest to the first number in the array.
Loop Through the Array
We will loop through the array and.
- If a number is bigger than largest, we update largest.
- If a number is smaller than smallest, we update smallest.
Display the Results
Once we’ve looked at every number in the array, we will print the largest and smallest numbers.
Java Code Example
Here’s the code to find the largest and smallest elements in an array.
public class FindLargestAndSmallest {
public static void main(String[] args) {
// Create an array of numbers
int[] numbers = {12, 35, 1, 10, 34, 1};
// Call the method to find the largest and smallest numbers
findLargestAndSmallest(numbers);
}
public static void findLargestAndSmallest(int[] arr) {
// Set the first number as both largest and smallest
int largest = arr[0];
int smallest = arr[0];
// Loop through the rest of the array
for (int i = 1; i < arr.length; i++) {
// Update largest if we find a bigger number
if (arr[i] > largest) {
largest = arr[i];
}
// Update smallest if we find a smaller number
if (arr[i] < smallest) {
smallest = arr[i];
}
}
// Print the largest and smallest numbers
System.out.println("Largest number in the array is: " + largest);
System.out.println("Smallest number in the array is: " + smallest);
}
}
How does the Code Work?
- Array: We create an array of numbers.
- Variables: We set the first number as both largest and smallest.
- Loop: We go through each number in the array and compare it to the current largest and smallest values.
- Result: After looping through the array, we print the largest and smallest numbers.
If you run the code with the array {12, 35, 1, 10, 34, 1}, the output will be.
Conclusion
In this article, we learned how to find the largest and smallest numbers in an array in Java. We used a simple approach where we start by assuming the first number is both the largest and smallest, then update those values as we loop through the array. This method is easy to understand and works well for any array.