0

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]);}

  }

任何帮助都会很棒。谢谢!

4

5 回答 5

8

由于您使用的是数组,因此您必须提前预测条目数。您的预测似乎已关闭,因此一些数组元素保留null.

请考虑使用ArrayList而不是原始数组。排序是用Collections.sort.

于 2013-06-26T11:37:52.143 回答
1

Use an Arraylist as then you dont have to estimate the size of your array as the ArrayList grows dynamically as you add more strings, your code would go something like this

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);
List<String> list1 = new ArrayList<String>()
List<String> list2 = new ArrayList<String>()

while(sc1.hasNextLine())
   list1.add(sc1.nextLine().toLowerCase());  //edited -- bad approach but would work if case not important


while(sc2.hasNextLine()){
   list2.add(sc2.nextLine().toLowerCase());  //edited -- bad approach but would work if case not important

Collections.sort(list1);
Collections.sort(list2);
for(String s: list1)
   System.out.println(s);

Or you could do this to implement a case insensitive sort, which would be better then altering the string as you add it to array

Collections.sort(list1, new Comparator<Object>() 
{
     @Override
     public int compare(Object o1, Object o2) 
     {
         String s1 = (String) o1;
         String s2 = (String) o2;
         return s1.compareToIgnoreCase(s2);
    }
}

Then repeat for list2. But an even better way would be to write a new comparator method as such

public class SortIgnoreCase implements Comparator<Object> {
    public int compare(Object o1, Object o2) {
        String s1 = (String) o1;
        String s2 = (String) o2;
        return s1.compareToIgnoreCase(s2);
    }
}

then call Collections.sort(list1, new SortIgnoreCase()); which is a cleaner way to write the code to sort multiple lists

于 2013-06-26T11:43:06.270 回答
1

如果您的数组对于数据来说太大了,那么您将null在未使用的元素中使用默认值;这些null值将导致您的异常。

在加载和排序数组之前,在所有元素中放置空白:

Arrays.fill(a1, "");
Arrays.fill(a2, "");
于 2013-06-26T11:43:36.870 回答
0

如果您的 Array 没有 null 值,则Arrays.sort()可以正常工作。

所以最好使用List来避免这种情况。

然后您可以将您的数组转换为ArrayList并用于Collections.sort()对它们进行排序。

    String[] str1 = {"Cat","Lam","Orange","Kam","Ramveer","None","Tue","Apple"};
    String[] str2 = {"Dog","Elephant","Kam","Monday","Parrot","Queen","Ramveer","Tuesday","Xmas"};
    List<String> lst1=Arrays.asList(str1);
    List<String> lst2=Arrays.asList(str2);
    Collections.sort(lst1);
    Collections.sort(lst2);
    System.out.println(lst1+"\n"+lst2);
于 2013-06-26T12:10:25.517 回答
0

如果您真的想使用数组,请使用它来阅读:

    List<String> list1 = new ArrayList();
    while (sc1.hasNextLine()) {
        list1.add(sc1.nextLine());
    }
    String[] ar1 = list1.toArray(new String[list1.size()]);

如果您可以使用 List 集合,那么:

    List<String> list1 = new ArrayList();
    while (sc1.hasNextLine()) {
        list1.add(sc1.nextLine());
    }
    Collections.sort(list1);
于 2013-06-26T11:44:18.127 回答