I want to sort an arraylist by sales person name but I'm not allowed to use built-in sort.
In the followin code, I'm trying to manually sort it but it shows error (required: variable found: value) on the lines commented.
我想按销售人员名称对arraylist进行排序,但我不允许使用内置排序。在下面的代码中,我试图手动对它进行排序,但它在评论的行上显示错误(必需:变量found:value)。
int j,k;
boolean flag = true;
Salesperson person = new Salesperson(null, 0, 0);
while (flag) {
flag = false;
for (j = 0, k=1; j < salesperson.size()-1; j++, k++) {
if (salesperson.get(j).getName().compareToIgnoreCase(salesperson.get(j+1).getName()) > 0) { // ascending sort
person = salesperson.get(j);
// salesperson.get(j) = salesperson.get(k);
// salesperson.get(k) = person;
flag = true;
}
}
}
3 个解决方案
#1
you can't sort an array in one loop , at least that's what i know , even if you got the part inside the if
statement right , the algorithm is still wrong . You only compared each person with the adjacent person in one pass only.
你不能在一个循环中对一个数组进行排序,至少这就是我所知道的,即使你在if语句中得到了正确的部分,算法仍然是错误的。您只在一次通过中将每个人与相邻的人进行比较。
Let's say you are sorting an array of numbers that looks like this :
假设您正在排序一组数字,如下所示:
5 4 6 3 1 2
with this algorithm , you would have this result 4 5 3 1 2 6
5 4 6 3 1 2使用此算法,你会得到这个结果4 5 3 1 2 6
on one pass like you did it's still not sorted , only every number got compared to the adjacent one.
像你一样,它仍然没有排序,只有每个数字与相邻的数字相比。
Also about setting a value , use ArrayList.set()
like Eran answered .
关于设置值,使用像Eran回答的ArrayList.set()。
Here are some sorting algorithms
这是一些排序算法
#2
//salesperson.get(j) = salesperson.get(k);
// salesperson.get(k) = person;
You are taking out the values through the get() method. That can't be used to assign values. That is why you get compilation errors.
您正通过get()方法取出值。这不能用于分配值。这就是你得到编译错误的原因。
What you should do is create a temp object and assigned to that and do the swapping of objects But you must recheck your sorting iteration logic
你应该做的是创建一个临时对象并分配给它并进行对象的交换但你必须重新检查你的排序迭代逻辑
#3
This is the correct code. I was making mistake in theses lines
这是正确的代码。我在这些线路上犯了错误
//salesperson.get(j) = salesperson.get(k);
// salesperson.get(k) = person;
these were replaced by the following lines, and now there is no error
这些被以下行代替,现在没有错误
salesperson.set(j, salesperson.get(k));
salesperson.set(k, person);
Thanks for your answers
谢谢你的回答
int j,k;
boolean flag = true; // will determine when the sort is finished
Salesperson person = new Salesperson(null, 0, 0);
while (flag) {
flag = false;
for (j = 0, k=1; j < salesperson.size()-1; j++, k++) {
if (salesperson.get(j).getName().compareToIgnoreCase(salesperson.get(j+1).getName()) > 0) { // ascending sort
person = salesperson.get(j);
salesperson.set(j, salesperson.get(k));
salesperson.set(k, person);
flag = true;
}
}
}
#1
you can't sort an array in one loop , at least that's what i know , even if you got the part inside the if
statement right , the algorithm is still wrong . You only compared each person with the adjacent person in one pass only.
你不能在一个循环中对一个数组进行排序,至少这就是我所知道的,即使你在if语句中得到了正确的部分,算法仍然是错误的。您只在一次通过中将每个人与相邻的人进行比较。
Let's say you are sorting an array of numbers that looks like this :
假设您正在排序一组数字,如下所示:
5 4 6 3 1 2
with this algorithm , you would have this result 4 5 3 1 2 6
5 4 6 3 1 2使用此算法,你会得到这个结果4 5 3 1 2 6
on one pass like you did it's still not sorted , only every number got compared to the adjacent one.
像你一样,它仍然没有排序,只有每个数字与相邻的数字相比。
Also about setting a value , use ArrayList.set()
like Eran answered .
关于设置值,使用像Eran回答的ArrayList.set()。
Here are some sorting algorithms
这是一些排序算法
#2
//salesperson.get(j) = salesperson.get(k);
// salesperson.get(k) = person;
You are taking out the values through the get() method. That can't be used to assign values. That is why you get compilation errors.
您正通过get()方法取出值。这不能用于分配值。这就是你得到编译错误的原因。
What you should do is create a temp object and assigned to that and do the swapping of objects But you must recheck your sorting iteration logic
你应该做的是创建一个临时对象并分配给它并进行对象的交换但你必须重新检查你的排序迭代逻辑
#3
This is the correct code. I was making mistake in theses lines
这是正确的代码。我在这些线路上犯了错误
//salesperson.get(j) = salesperson.get(k);
// salesperson.get(k) = person;
these were replaced by the following lines, and now there is no error
这些被以下行代替,现在没有错误
salesperson.set(j, salesperson.get(k));
salesperson.set(k, person);
Thanks for your answers
谢谢你的回答
int j,k;
boolean flag = true; // will determine when the sort is finished
Salesperson person = new Salesperson(null, 0, 0);
while (flag) {
flag = false;
for (j = 0, k=1; j < salesperson.size()-1; j++, k++) {
if (salesperson.get(j).getName().compareToIgnoreCase(salesperson.get(j+1).getName()) > 0) { // ascending sort
person = salesperson.get(j);
salesperson.set(j, salesperson.get(k));
salesperson.set(k, person);
flag = true;
}
}
}