1. 使用`Arrays.sort()`方法:
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 3, 1};
Arrays.sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
}
2. 使用`Arrays.parallelSort()`方法(Java 8及以上版本):
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 3, 1};
Arrays.parallelSort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
}
3. 使用自定义比较器`Comparator`:
import java.util.Arrays;
import java.util.Comparator;
public class SortArray {
public static void main(String[] args) {
Integer[] arr = {5, 2, 8, 3, 1};
Arrays.sort(arr, new Comparator
() { @Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
for (int i : arr) {
System.out.print(i + " ");
}
}
}
以上代码示例展示了如何使用Java内置的排序方法对整数数组进行升序排列。您可以根据需要选择合适的方法