JAVA对数组合并后排序_用java中的数组列表合并排序

因此,为了做作业,我必须编写一个程序,将mergeSorts与来自与常规数组一起工作的代码中的数组列表合并,我只是想知道是否有人可以帮我弄清楚我出错的地方,因为我的代码抛出了大量的NULL POINTER异常,我试图修复它们,但是当我修复它时会去另一个......等等......

谢谢!

我的代码:

private static ArrayList numbers= new ArrayList();

private static ArrayList helper;

private static int number;

public static void sort(ArrayList myNumbers){

for(int i=0; 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

//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

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

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值