我正在开发一种排序可视化算法。我快完成了,但此错误不断弹出。 (在封闭范围内定义的局部变量i必须是final或实际上是final)。我该如何解决?
public void SelectionSort() throws InterruptedException{
Timer timer = new Timer();
int i = 0;
for (i = 0; i < array.length - 1; i++){
int index = i;
for (int j = i + 1; j < array.length; j++){
if (array[j] < array[index]){
index = j; // searching for lowest index
}
}
timer.schedule(new TimerTask() {
public void run() {
swap(i, index);
}
}, 100, 500);
}
}
private void swap(int i, int index) {
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
repaint();
}
错误发生在swap(i,index);