若要对Short数组进行排序,请使用Arrays.sort()方法。
让我们首先声明并初始化一个未排序的Short数组。
short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };
现在,让我们缩短数组。
Arrays.sort(arr);
以下是显示Java中如何对Short数组进行排序的完整示例。
import java.util.*; public class Demo { public static void main(String []args) { short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 }; System.out.println("Unsorted:"); for (short s : arr) { System.out.println(s); } System.out.println("Sorted:"); Arrays.sort(arr); for (short s : arr) { System.out.println(s); } System.out.println(); } }
输出结果
Unsorted: 35 25 18 45 77 21 3 Sorted: 3 18 21 25 35 45 77