问题是您还使用了double来索引数组.所以试试这个:
public static void selectionSort (double...arr) { int i = 0, j = 0, smallest = 0; double temp = 0.0; for (i = 0; i < arr.length - 1; i++) { smallest = i; for (j = 1; j < arr.length - 1; j++) { if (arr[j] < arr[smallest]) smallest = j; } temp = arr[smallest]; arr[smallest] = arr[i]; arr[i] = temp; } }
正如您所看到的,仍然有双精度作为参数,temp
-value和arr
-array也仍然是双精度数,但用于数组的索引是整数.
索引总是int.例如,当我们有一个字符串数组时,我们仍然使用int作为索引:
String[] sArray = { "word1", "word2", "word3" } int index = 1; String result = sArray[index]; // As you can see we use an int as index, and the result is a String // In your case, the index is still an int, but the result is a double