我有一个字符数组
String a = "badabcde";
char[] chArr = a.toCharArray(); // 'b','a','d','a','b','c','d','e'
给定开始索引和结束索引,仅对数组的一部分排序的最简单方法是什么?
// 'b','a','d','a','b','c','d','e'
subSort(array, startIndex, endIndex);
Ex:
subSort(chArr, 2, 5);
// 'b','a','a','b','c','d','d','e' // sorts indices 2 to 5
最佳答案
我认为public static void sort(char[] a, int fromIndex, int toIndex)回答了您的问题。
String a = "badabcde";
char[] chArr = a.toCharArray(); // 'b','a','d','a','b','c','d','e'
// fromIndex - the index of the first element (inclusive) to be sorted
// toIndex - the index of the last element (exclusive) to be sorted
Arrays.sort(chArr,2,6);