2015-02-07 132 views
4

的款我有個字符爪哇 - 排序只陣列

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 

回答

11

我認爲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);