让我们首先创建一个字符串数组-
String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };
现在,使用Arrays.sort()获取子集。使用以下内容仅对索引范围2到6进行排序。
Arrays.sort(strArr, 2, 6);
import java.util.Arrays; public class Demo { public static void main(String[] args) { String[] strArr = new String[] { "r", "p", "v","y", "s", "q" }; Arrays.sort(strArr, 2, 6); System.out.println("Sorted subset of array elements from index 2 to 6..."); for (int a = 0; a < strArr.length; a++) { System.out.println(strArr[a]); } } }
输出结果
Sorted subset of array elements from index 2 to 6... r p q s v y