Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 插入排序排序一个ArrayList问题_Java_Arraylist_While Loop_Insertion Sort - Fatal编程技术网

Java 插入排序排序一个ArrayList问题

Java 插入排序排序一个ArrayList问题,java,arraylist,while-loop,insertion-sort,Java,Arraylist,While Loop,Insertion Sort,现在我已经做了一段时间了,我有一个错误。现在我正在做的程序是一个地址簿,我正在使用插入排序对我称为books(地址条目)的对象的arraylist进行排序。现在我很快发现我的分拣机没有正确分拣,所以我做了一个简单的程序来测试分拣机,但它还是不工作。我想知道你们能不能看一下,帮我解决一下 这是我的分拣机: import java.util.ArrayList; public class Sorts { /** * Sorts and array of integer from

现在我已经做了一段时间了,我有一个错误。现在我正在做的程序是一个地址簿,我正在使用插入排序对我称为books(地址条目)的对象的arraylist进行排序。现在我很快发现我的分拣机没有正确分拣,所以我做了一个简单的程序来测试分拣机,但它还是不工作。我想知道你们能不能看一下,帮我解决一下

这是我的分拣机:

import java.util.ArrayList;
public class Sorts {

    /**
     * Sorts and array of integer from low to high
     * pre: none
     * post: Integers has been sorted from low to high
     */
    public static void insertionSort(ArrayList<String> test) {
        Comparable temp;
        int previousIndex;
        ArrayList<String> objectSort = test;

        for (int i = 1; i < objectSort.size(); i++) {
            temp = objectSort.get(i);
            previousIndex = i - 1;

            while ((objectSort.get(previousIndex).compareTo((String) temp)) == 1 && (previousIndex > 0)) {
                objectSort.set(previousIndex + 1, objectSort.get(previousIndex));
                previousIndex -= 1; //decrease index to compare current item with next previous item
            }
            if (objectSort.get(previousIndex).compareTo((String) temp) == 1) {
                /* shift item in first element up into next element */
                objectSort.set(previousIndex + 1, objectSort.get(previousIndex));
                /* place current item at index 0 (first element */
                objectSort.set(previousIndex, (String) temp);
            } else {
                /* place current item at index ahead of previous item */
                objectSort.set(previousIndex + 1, (String) temp);
            }

        }
    }
}
import java.util.ArrayList;
公共类分类{
/**
*整数从低到高的排序和数组
*pre:无
*post:整数已从低到高排序
*/
公共静态void insertionSort(ArrayList测试){
可比温度;
int-previousIndex;
ArrayList objectSort=test;
对于(int i=1;i0)){
objectSort.set(previousIndex+1,objectSort.get(previousIndex));
previousIndex-=1;//减少索引以将当前项目与上一个项目进行比较
}
if(objectSort.get(previousIndex).compareTo((字符串)temp)==1){
/*将第一个元素中的项目上移到下一个元素中*/
objectSort.set(previousIndex+1,objectSort.get(previousIndex));
/*将当前项放在索引0(第一个元素)处*/
objectSort.set(previousIndex,(String)temp);
}否则{
/*将当前项放在索引中前一项的前面*/
objectSort.set(上一个索引+1,(字符串)temp);
}
}
}
}
我测试它的简单程序是:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args){
        ArrayList<String> test = new ArrayList<String>();

        test.add("Roxy");
        test.add("Proxy");
        test.add("Moxy");
        test.add("Samuel Adams");

        Sorts.insertionSort(test);

        System.out.println(test);

    }

}
import java.util.ArrayList;
公共班机{
公共静态void main(字符串[]args){
ArrayList测试=新的ArrayList();
测试。添加(“Roxy”);
测试。添加(“代理”);
测试。添加(“Moxy”);
测试。添加(“Samuel Adams”);
排序。插入排序(测试);
系统输出打印LN(测试);
}
}

总而言之,我的ArrayList排序器有问题。问题是它无法正确排序,我不知道为什么。提前非常感谢。如果您有任何问题,请随时提问。:)

第一个问题:您希望
compareTo
总是为“大于”返回1。它只返回一个大于0的值,该值可能是不同的正整数。因此,您的
==1
比较应该是
>0


可能还有其他问题,但这是我首先考虑的问题。

您能显示预期输出和实际输出吗?您为什么不使用
集合。排序(测试)?这是学校作业。我选择制作一本通讯录。先决条件之一是使用分类器或查找器,但是它们需要定制,我不能使用java中包含的类。我曾经使用插入排序器来处理阵列,它工作得非常好。然而,我决定使用arraylist使我的程序更加通用和高效。没问题,欢迎使用!嘿,非常感谢你,乔恩。它工作得很好。我会在5分钟后点击接受你的答案。不过,再次感谢您。:)这也是我项目的救命稻草!非常感谢。