Sorting 冒号排序不是排序-IntDoublePair
为了对IntDoublePair进行排序,我实现了冒泡排序。例如:Sorting 冒号排序不是排序-IntDoublePair,sorting,hadoop,Sorting,Hadoop,为了对IntDoublePair进行排序,我实现了冒泡排序。例如: [1 0.5] [1 0.8] [1 0.67] 分类如下: [1 0.5] [1 0.67] [1 0.8] 当我执行代码时,它给了我没有按顺序排序的数据。我很困惑,我到底错在哪里。我需要一些帮助 private ArrayList<IntDoubleTextPair> sortCollection(ArrayList<IntDoubleTextPair> collection) {
[1 0.5]
[1 0.8]
[1 0.67]
分类如下:
[1 0.5]
[1 0.67]
[1 0.8]
当我执行代码时,它给了我没有按顺序排序的数据。我很困惑,我到底错在哪里。我需要一些帮助
private ArrayList<IntDoubleTextPair> sortCollection(ArrayList<IntDoubleTextPair> collection)
{
for (int current = 0; current < collection.size(); current++)
{
for (int next = 1; next < collection.size(); next++)
{
if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
{
if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
{
temp = collection.get(next);
collection.set(next, collection.get(current));
collection.set(current, temp);
}
}
}
}
return collection;
}
专用ArrayList sortCollection(ArrayList集合)
{
对于(int current=0;current=collection.get(下一个).getFirst().get())
{
if(collection.get(当前).getSecond().get()>collection.get(下一个).getSecond().get())
{
temp=collection.get(下一步);
set(下一步,collection.get(当前));
采集设置(当前、温度);
}
}
}
}
回收;
}
我认为您可能有一些错误的逻辑:
if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
{
if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
{
temp = collection.get(next);
collection.set(next, collection.get(current));
collection.set(current, temp);
}
}
使用这两个嵌套的if语句,在本例中不会发生切换,但它需要:
[2 .1]
[1 .9]
因为即使第一个if语句的计算结果为true,第二个if语句也会失败,因为它只查看第二个数字。也许这会更好:
if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
{
if (collection.get(current).getFirst().get() == collection.get(next).getFirst().get())
{
if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
{
temp = collection.get(next);
collection.set(next, collection.get(current));
collection.set(current, temp);
}
}
else
{
temp = collection.get(next);
collection.set(next, collection.get(current));
collection.set(current, temp);
}
}
快乐编码!如果您有任何问题,请留下评论。我认为排序应该至少适用于您正在传递的数组(尽管嵌套if条件中存在问题),但在交换操作中向temp传递引用时可能会出现问题,因此请尝试创建temp对象,而不是传递引用传递值:
private ArrayList<IntDoubleTextPair> sortCollection(ArrayList<IntDoubleTextPair> collection)
{
for (int current = 0; current < collection.size(); current++)
{
for (int next = 1; next < collection.size(); next++)
{
if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
{
if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
{
IntDoubleTextPair temp= new IntDoubleTextPair();
temp.getFirst() = collection.get(next).getFirst();
temp.getSecond() = collection.get(next).getSecond();
collection.set(next, collection.get(current));
collection.set(current, temp);
}
else if(collection.get(current).getFirst().get() != collection.get(next).getFirst().get()){
IntDoubleTextPair temp= new IntDoubleTextPair();
temp.getFirst() = collection.get(next).getFirst();
temp.getSecond() = collection.get(next).getSecond();
collection.set(next, collection.get(current));
collection.set(current, temp);
}
}
}
}
return collection;
}
专用ArrayList sortCollection(ArrayList集合)
{
对于(int current=0;current=collection.get(下一个).getFirst().get())
{
if(collection.get(当前).getSecond().get()>collection.get(下一个).getSecond().get())
{
IntDoubleTextPair temp=新的IntDoubleTextPair();
temp.getFirst()=collection.get(next.getFirst();
temp.getSecond()=collection.get(next.getSecond();
set(下一步,collection.get(当前));
采集设置(当前、温度);
}
else if(collection.get(当前).getFirst().get()!=collection.get(下一个).getFirst().get()){
IntDoubleTextPair temp=新的IntDoubleTextPair();
temp.getFirst()=collection.get(next.getFirst();
temp.getSecond()=collection.get(next.getSecond();
set(下一步,collection.get(当前));
采集设置(当前、温度);
}
}
}
}
回收;
}
仍然无法正确排序。外循环和内循环初始化是否正确?我已经按照您所说的重新初始化了temp对象,但是没有运气。我想,collection.set()方法应该有问题,您是否调试并看到它工作正常?您可能需要重写该方法。或者,您可以用创建临时对象的相同方法更改当前对象和下一个对象的第一个和第二个值:使用浅拷贝交换可能没问题,是吗?我不认为完全有必要为temp创建新的IntDoubleTextPair。不过我可能弄错了。