It's a new content, but I'll make a note to remember it.
If you declare an array and make it Arrays.sort (reference variable), the sort operation is over.
By the way, it is sorted only in ascending order (in the case of value, in ascending order). If you want to get several values in descending order, you have to devise a for () statement after sorting to get the values.
If you want to output as a character string expression with [], use Arrays.toString () below, but the return value will be a String type. To get the values of the sorted array after that, the operation of lists [i] is required.
The sorting method of the Arrays class is Dual-Pivot Quicksort, which is faster than general quicksorting, so I think it's better to prioritize this without hesitation rather than implementing bubble sort and making mistakes yourself.
java
//what if{3,9,2,7,5}If so
int[] lists = new int[5];
//Sorting is complete at this point
Arrays.sort(lists);
//{2,3,5,7,9}Next list[0]Get 2
System.out.println(lists[0]);
//[1,2,3,4,5]Returns a string representation like
Arrays.toString(a);
//Output all sorted array
for(int list:lists){
System.out.println(list+",");
}
Recommended Posts