重点:引用类型变量的数组如何交换
错误代码:
for(int index = 0;index < shapes.length - 1 ;index++){
// int aimNum = shapes[index].getC();
Shape aimShape = shapes[index];
for(int scan = index + 1;scan < shapes.length;scan++){
if(compare(aimShape ,shapes[scan])){
aimShape = shapes[scan];
}
}//如何进行交换
temp = shapes[index];
shapes[index] = aimShape;
aimShape = temp;
错误原因:选出最小值后,要进行交换,没搞清楚引用类型的交换是对象地址的交换,根源上还是要进行数组索引处的交换,最后要回到数组上
for(int index = 0;index < shapes.length - 1 ;index++){
// int aimNum = shapes[index].getC();
minIndex = index;
Shape aimShape = shapes[index];
for(int scan = index + 1;scan < shapes.length;scan++){
if(compare(aimShape ,shapes[scan])){
aimShape = shapes[scan];
minIndex = scan;
}
}//如何进行交换
temp = shapes[index];
shapes[index] = aimShape;
shapes[minIndex] = temp;
}
}
对数组对象的改变:最终一定要回答数组上,通过指针(index)实现对数组对象的改变