0

因此,对于家庭作业,我必须编写一个程序,将与常规数组一起使用的代码中的数组列表合并排序,我只是想知道是否有人可以帮助我找出我出错的地方,因为我的代码抛出了大量的 NULL POINTER 异常,我试图修复它们,但是当我修复一个时,它会转到另一个......等等......

谢谢!我的代码:

private static ArrayList<Integer> numbers= new ArrayList<Integer>();
private static ArrayList<Integer> helper;
private static int number;
public static void sort(ArrayList<Integer> myNumbers){
    for(int i=0; i<myNumbers.size();i++){
        numbers.add(myNumbers.get(i));
    }
    //numbers=myNumbers;
    number = myNumbers.size()-1;

    mergesort(0, number -1);
}
private static void mergesort(int low, int high){
    //check if low is smaller than high, if not then the array is sorted
    if(low<high){
        //get the index of the element which is in the middle
        int middle=low+(high-low)/2;
        //sort the left side of the array
        mergesort(low, middle);
        //sort the right side of the array
        mergesort(middle +1, high);
        //combine them both
        merge(low, middle, high);
    }
}
private static void merge(int low, int middle, int high){
    //copy both parts into the helper array
    for(int i=high;i>low;i++){
        helper.add((numbers.get(i)));
    }

    int i=low;
    int j=middle+1;
    int k=low;
    //copy the smallest myNumbers from either the left or right side back to the original array
    while(i<middle  && j<high){
        if(helper.get(i)< helper.get(j)){
            numbers.set(k,(helper.get(i)));
            i++;
        }
        else{
            numbers.set(k,(helper.get(j)));
            j++;
        }
        k++;
    }
    //copy the rest of the left side of the array into target array
    while(i<middle){
        numbers.set(k,helper.get(i));
        k++;
        i++;
    }
}

回报:

Exception in thread "main" java.lang.NullPointerException
at BinarySearch.merge(BinarySearch.java:61)
at BinarySearch.mergesort(BinarySearch.java:55)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.sort(BinarySearch.java:43)
at BinarySearch.main(BinarySearch.java:25)
4

5 回答 5

1

它尝试访问未创建的合并函数中的辅助字段。

另外我认为您应该从所有函数/字段中删除静态关键字。

私有静态 ArrayList 助手;

私有静态 ArrayList helper= new ArrayList();

于 2013-04-25T12:05:45.683 回答
1

这是罪魁祸首:

for(int i=high;i>low;i++){
    helper.add((numbers.get(i)));
}

改为使用for(int i=high; i>=low; i--) {

于 2013-04-25T12:09:50.017 回答
0

blackuprise 指出了你的两个问题。

您的合并实现还有另一个问题,这可能会使您的合并结果错误。在您的合并方法结束时,您有:

//copy the rest of the left side of the array into target array
while...

你怎么能确定每次数组的所有右侧都会被合并,只有左边的部分有元素?(奇怪的句子^_^ left part has sth. left。)

您也应该检查右侧。或使用 aSENTINEL来避免“休息”检查。

于 2013-04-25T12:19:10.757 回答
0

如果您正在尝试使用您的算法实现合并排序以用于学术用途,并且给定的答案是一个好的答案,否则如果您对 Guava 已经拥有的实现感兴趣,那么这里是:guava Iterators

于 2013-04-25T12:22:33.350 回答
0

公共类合并

public  static void sort(List<String>result )
{

     for( i=0;i<result.size();i++)
     {
        try{

          try{


         if(result.get(i).compareTo(result.get(i+1))>0)
             {
             String aux=result.get(i);
             result.set(i, result.get(i+1));
             result.set(i+1, aux);

             }


             }catch(NullPointerException e){}
          }catch(ClassCastException z){}

        }
    }


public static void main(String[]args) throws ClassNotFoundException
{ 
    String[]resultentry=new String[100];
    int index;//nr de elemente din vectorul de stringuri//
    int i;

    try{
   //declare files and lists//
    BufferedReader firstfile;
    BufferedReader secondfile;


    firstfile=new BufferedReader(new FileReader("lista1.txt"));
    secondfile=new BufferedReader(new FileReader("lista2.txt"));;


         firstentry=firstfile.readLine();
        secondentry=secondfile.readLine();

        while(firstentry!=null&&secondentry!=null)
        {
        firstlist.add(firstentry);
        firstentry=firstfile.readLine();

        secondlist.add(secondentry) ;
        secondentry=secondfile.readLine();

         }


     }catch(IOException exp){}


     try{

    java.util.Collections.sort(firstlist);
    System.out.println("First list sorted :"+ firstlist);

    java.util.Collections.sort(secondlist);
    System.out.println("Second list sorted"+secondlist);


    firstlist.addAll(secondlist);
    java.util.Collections.sort(firstlist);

    System.out.println("Result  list sorted "+" "+firstlist);

    }catch(ClassCastException b){}

   }

}`
于 2015-07-17T21:25:23.050 回答