package org.kodejava.util;
import java.util.Arrays;
public class ArraySortExample {
public static void main(String[] args) {
// An array of random numbers
int[] numbers = {3, 1, 8, 34, 1, 2, 13, 89, 5, 21, 55};
System.out.println("Before: " + Arrays.toString(numbers));
// We need to sort these array elements into a correct order
// from the smallest to the greatest. We will use the Arrays
// class on java.utils package to do the sort. The sort
// method of this class are overloaded, so they can take
// other type of array as well such as byte[], long[],
// float[], Object[].
Arrays.sort(numbers);
System.out.println("After : " + Arrays.toString(numbers));
// We can also do the sort only for the specified range of
// array elements.
float[] money = {1.05f, 99.8f, 3f, 4.55f, 7.23f, 6.50f};
Arrays.sort(money, 3, money.length);
// Here we display the sort result, the first and the second
// element of the array is not included in the sort process.
System.out.println("Money : " + Arrays.toString(money));
}
}
And here are the results:
Before: [3, 1, 8, 34, 1, 2, 13, 89, 5, 21, 55]
After : [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Money : [1.05, 99.8, 3.0, 4.55, 6.5, 7.23]
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024