我正在尝试对一个有7列的2D ArrayList进行冒泡排序,其中内部列表的第三列是价格。我正在尝试比较行的价格列,并交换具有更高价格的行与具有较低价格的行。这意味着最终ArrayList应该按价格升序排列。
但每次交换行时,较大行中的所有元素都会更改为与较小行中相同的元素。以下是代码。
boolean found = true;
do{
found = false;
for(int i = 0; i <= rc; i++) {
if(i + 1 <= rc) {
if(Integer.parseInt(list.get(i).get(3)) > Integer.parseInt(list.get(i + 1).get(3))) {
ArrayList<String> greater = list.get(i);
ArrayList<String> smaller = list.get(i + 1);
for(int k = 0; k <= 7; k++) {
list.get(i).set(k, smaller.get(k));
list.get(i + 1).set(k, greater.get(k));
}
found = true;
}
}
}
} while(found == true);
原始数组列表:
[[1, sagarmatha, 5000, 7000, Two-Star, Two-Person-Room, 2, Resturant],
[2, barahi, 4000, 4000, Three-Star, One-Person-Room, 1, Free-WIFI]]
排序后:
[[2, barahi, 4000, 4000, Three-Star, One-Person-Room, 1, Free-WIFI],
[2, barahi, 4000, 4000, Three-Star, One-Person-Room, 1, Free-WIFI]]