Home >Java >javaTutorial >How Can I Sort an Array of Integers in Ascending Order Using Java?
Sorting an Array in Java
In your Java program, you have an array of 10 integers with random values that you wish to sort in ascending order. To do so, you can utilize Java's built-in sorting method.
To sort the array, add the following line before the println statement:
Arrays.sort(array);
The Arrays class provides various sorting methods, and sort is specifically designed to sort primitive arrays. It accepts an array as its argument and arranges its elements in ascending order.
With this addition, your code will now sort the array before printing it onto the screen. The sorted values will be displayed in the correct order.
The above is the detailed content of How Can I Sort an Array of Integers in Ascending Order Using Java?. For more information, please follow other related articles on the PHP Chinese website!