The following program demonstrates how to Display the Two Largest and Two Smallest Elements in an Array in Java.

Basically, we find the first largest and first smallest elements as follows. We take two variables maximum, and minimum, and assign the first element of the array to both of them. After that, in a loop, we compare all the subsequent elements with the value of minimum and maximum. In case, the array element is greater than the maximum or less than the minimum, we change the corresponding value of maximum or minimum.

In order to find the second largest element and the second smallest element, we use another for loop. Like before, we initialize the two variables with the first element of the array. However, in the for loop, we compare the second largest with the value of the current element of the array. Also, we need to check whether the array element is smaller than the maximum value. If both of these conditions satisfy, we assign the current array element to the second largest. Similarly, we find the value of second smallest element.