我目前有3个阵列,Letter,AvgX,AvgY。 所有数组中的元素位置都相同,我使用冒泡排序来对avgX值进行排序,代码同时移动其他元素。
ArrayList<String> letter = new ArrayList<>(); // Array List used for storing the letters, average x+y values.
ArrayList<Integer> avgX = new ArrayList<>(); // Array List used as it is dynamic, instally it starts with 10 blank elements.
ArrayList<Integer> avgY = new ArrayList<>();
int i = 0, n = avgX.size();
boolean more_swaps = true;
while (i < n - 1 && more_swaps) {
more_swaps = false;
for (int j = 1; j < n - i; j++) {
if (avgX.get(j - 1) > avgX.get(j)) {
int temp = avgX.get(j - 1);
avgX.set(j - 1, avgX.get(j));
avgX.set(j, temp);
String Letter_temp = Letter.get(j - 1);
Letter.set(j - 1, Letter.get(j));
Letter.set(j, Letter_temp);
int temp_Y = avgY.get(j - 1);
avgY.set(j - 1, avgY.get(j));
avgY.set(j, temp_Y);
more_swaps = true;
}
}
if (!more_swaps) {
Log.d("bubble sort", "AvgX, AvgY, letters have been sorted");
more_swaps = false;
}
i++;
}
}
我想使用一组自定义对象将它们合并在一起。我不知道如何声明一个以及我需要在冒泡排序中改变什么?