java中与数组列表合并排序
因此,作为家庭作业,我必须编写一个程序,将常规数组代码中的排序与数组列表合并,我只是想知道是否有人能帮我找出哪里出了问题,因为我的代码抛出了大量空指针异常,我曾试着修复它们,但当我修复一个时,它会转到另一个……等等 谢谢! 我的代码:java中与数组列表合并排序,java,arraylist,mergesort,Java,Arraylist,Mergesort,因此,作为家庭作业,我必须编写一个程序,将常规数组代码中的排序与数组列表合并,我只是想知道是否有人能帮我找出哪里出了问题,因为我的代码抛出了大量空指针异常,我曾试着修复它们,但当我修复一个时,它会转到另一个……等等 谢谢! 我的代码: 它尝试访问未创建的合并函数中的帮助器字段 此外,我认为您应该从所有函数/字段中删除static关键字 私有静态ArrayList助手 到 private static ArrayList helper=new ArrayList() 罪魁祸首是: for(int
它尝试访问未创建的合并函数中的帮助器字段 此外,我认为您应该从所有函数/字段中删除static关键字 私有静态ArrayList助手 到 private static ArrayList helper=new ArrayList() 罪魁祸首是:
for(int i=high;i>low;i++){
helper.add((numbers.get(i)));
}
用
代替(inti=high;i>=low;i--){
。blackuprise指出了您的两个问题
合并实现存在另一个问题,这可能会导致合并结果错误。在合并方法的末尾,您有:
//copy the rest of the left side of the array into target array
while...
你怎么能这么肯定每次数组的右边都会被合并,只有左边的部分还有元素呢?(奇怪的句子^ ^左边的部分还有一些东西。)
你也应该检查右侧。或者使用
SENTINEL
来避免“休息”检查。如果您试图使用您的算法实现合并排序以用于学术用途,并且给出的答案是好的,或者如果您对Guava已经拥有的实现感兴趣,那么它就是:公共类合并
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){}
}
}`
公共静态无效排序(Listresult)
{
对于(i=0;i0)
{
字符串aux=result.get(i);
result.set(i,result.get(i+1));
结果集(i+1,辅助);
}
}捕获(NullPointerException e){}
}catch(ClassCastException z){}
}
}
公共静态void main(字符串[]args)引发ClassNotFoundException
{
String[]resultentry=新字符串[100];
int index;//nr de elemente din vectorul de stringuri//
int i;
试一试{
//声明文件和列表//
BufferedReader第一文件;
BufferedReader第二文件;
firstfile=new BufferedReader(新的文件阅读器(“lista1.txt”);
secondfile=newbufferedreader(newfilereader(“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){}
试一试{
java.util.Collections.sort(firstlist);
System.out.println(“排序的第一个列表:+firstlist”);
java.util.Collections.sort(secondlist);
System.out.println(“已排序的第二个列表”+第二个列表);
firstlist.addAll(第二个列表);
java.util.Collections.sort(firstlist);
System.out.println(“结果列表排序”+“”+firstlist);
}catch(ClassCastException b){}
}
}`
您的助手
未初始化。这会给您带来麻烦我无法删除static,因为它与我的main在同一个类中。如果您尝试多次运行它,numbers和helper将具有以前的值,因此您需要在再次运行它之前清除list,最好的方法是将此功能移动到一个新的类MergeSort,然后在main类中创建MergeSort的新实例并调用所需的函数。不确定您的意思,我还没有用java那么久。@user2319595 merge方法将合并两个子数组。如果幸运的话,在第一次while
之后,两个子数组没有剩下任何元素。然而,它们中的任何一个都可能有剩余的元素。您必须处理其余元素,然后将它们添加到目标/合并数组中。你只检查了左边,没有检查右边。看看这段代码,我有两个合并方法,有没有sentinel:@user2319595它取决于输入数组。正如我所说,如果你幸运的话,你的合并排序会给出正确的结果。但并非总是如此做一些测试用例,你会看到的。哈哈,谢谢!是的,我有一个程序,在一个文本文件中放入100个随机整数,并将其转换为一个数组列表供我的测试人员使用,但现在它返回一组0和数字26,大约是45的12倍,然后停止
//copy the rest of the left side of the array into target array
while...
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){}
}
}`