There are multiple solutions on how to find the smallest and largest number inside an array. We would be showing two ways, one which uses several methods readily available on the API. And the other which rely on basic usage of operators and loops.

Use of Sort to Identify Smallest and Largest Number in an Array

This method simply leveraged the existing functionality of the static method Arrays.sort. This method sort the specified array. The sort method arrange the elements from highest to lowest. So the first element of the resulting array would be our largest number while the last element would be the smallest. Lets take a look for below example

package com.javatutorialhq.java.examples;

import java.util.Arrays;

/*
 * This example source code demonstrates how
 * to find the smallest and largest number  
 * in an array. Shortcut method
 */

public class ArraySmallLargestExample {

	public static void main(String[] args) {

		// instantiate an array of numbers
		Double[] listDoubles = new Double[] { 1.2, 3.4, 0.25, 
				0.75, 0.12, 9.8, 1.75, 3.45 };
		
		Arrays.sort(listDoubles);
		Double smallest = listDoubles[0];
		Double largest = listDoubles[listDoubles.length-1];
		System.out.println("Smallest Number:"+smallest);
		System.out.println("Largest Number:"+largest);		

	}

}

Sample Output:

Smallest Number:0.12
Largest Number:9.8

Use of loops and Operators to find the smallest and largest number in an array

For this example in order to find the smallest and largest number, we have initially assigned a variable greatestNumber and  smallestNumber the first value of the array. And then we just use the operator < and > to check if the next number is less than or greater than the current value. From the result of the checking we change the value of the two variables.

package com.javatutorialhq.java.examples;


/*
 * This example source code demonstrates how
 * to find the smallest and largest number  
 * in an array. Long method
 */

public class ArraySmallLargestExample {

	public static void main(String[] args) {

		// instantiate an array of numbers
		Double[] listDoubles = new Double[] { 1.2, 3.4, 15.25, 0.75, 0.127, 10.5,
				1.75, 3.45 };
		Double greatestNumber = listDoubles[0];
		Double smallestNumber = listDoubles[0];
		for (Double d : listDoubles) {
			if (d > greatestNumber) {
				greatestNumber = d;
			}
			if (d < smallestNumber) {
				smallestNumber = d;
			}

		}
		System.out.println("Smallest Number:"+smallestNumber);
		System.out.println("Largest Number:"+greatestNumber);
		

	}

}

Sample Output:

Smallest Number:0.127
Largest Number:15.25