ar1
我在and中有两个字符串数组ar2
,我正在从文件中读取输入并存储在数组中,ar1
包含
Cat
Lam
Orange
Kam
Ramveer
None
Tue
Apple
ar2 包含
Dog
elephant
Kam
Monday
Parrot
Queen
Ramveer
Tuesday
Xmas
我正在尝试按字母顺序对数组进行排序,并且我正在使用Array.sort()
,但出现异常
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:232)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:176)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at CompareArrays.pr1(CompareArrays.java:51)
at CompareArrays.main(CompareArrays.java:86)
Java 结果:1 构建成功(总时间:0 秒)
代码
File file1= new File("C:\\Users\\Ramveer\\Desktop\\updates\\f1.txt");
File file2=new File("C:\\Users\\Ramveer\\Desktop\\updates\\f2.txt");
Scanner sc1=new Scanner(file1);
Scanner sc2=new Scanner(file2);
while(sc1.hasNextLine()){
ar1[c1]=sc1.nextLine();
c1++;
}
while(sc2.hasNextLine()){
ar2[c2]=sc2.nextLine();
c2++;
}
Arrays.sort(ar1);
for(int k=0;k<c1;k++){
System.out.println(ar1[k]);}
}
任何帮助都会很棒。谢谢!