【发布时间】:2017-11-05 10:39:05
【问题描述】:
首先,我正在编写程序来可视化 Android 上的排序算法 我写了插入排序算法的方法。我正在使用 MPAndroidChart。
void insertionSort(int[] arr) {
int i, j, newValue;
for (i = 1; i < arr.length; i++) {
newValue = arr[i];
j = i;
while (j > 0 && arr[j - 1] > newValue) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = newValue;
}
}
...它工作正常。我想重新设计它以使用 ArrayList 获得相同的结果,但不幸的是,它崩溃了,我不知道为什么!这是我的集合方法insertionSort:
void insertionSort(ArrayList<BarEntry> list) {
int i, j;
float newValue;
for (i = 1; i < list.size(); i++) {
newValue = list.get(i).getY();
j = i;
while (j > 0 && list.get(j-1).getY() > newValue) {
list.set(j, list.get(j-1));
j--;
}
BarEntry be = list.get(list.indexOf(newValue));
list.set(j, be);
}
}
BarEntry 是用于创建图表的类。我想通过它的 Y 坐标对我的数据进行排序,我通过 getY() 函数执行此操作。程序在那里关闭:
BarEntry = list.get(list.indexOf(newValue)); list.set(j, be);
简而言之: 我该如何替代
arr[j] = newValue;
这样的收集方法(不工作):
list.set(j, list.get(list.indexOf(newValue)));
【问题讨论】:
-
您是否收到异常消息?堆栈跟踪?
-
不,当我单击调用我的方法的按钮时,我的应用程序刚刚重新启动。
标签: java android algorithm sorting arraylist