使用string.equals比较java中的两个字符串数组

时间:2014-12-28 05:16:28

标签: java arrays string

我有一种方法可以正常工作并提供所需的结果。这是代码:

public class arraycomparison {

public static void main(String args[]) {
    try {

        Scanner sc1 = new Scanner(new FileInputStream("/Users/esna786/File1.txt"));
        Scanner sc2 = new Scanner(new FileInputStream("/Users/esna786/File2.txt"));
        List<String> File1Content = new ArrayList<String>();
        List<String> File2Content = new ArrayList<String>();
        List<String> commonWords = new ArrayList<String>();

        //Getting the contents of File1
        while (sc1.hasNext()) {
            File1Content.add(sc1.next());
        }
        String arr1[] = File1Content.toArray(new String[File1Content.size()]);

        //Getting the contents of File1
        while (sc2.hasNext()) {
            File2Content.add(sc2.next());
        }
        String arr2[] = File2Content.toArray(new String[File2Content.size()]);

        int count = 0;

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    commonWords.add(arr1[i]);
                    count++;
                }
            }
        }

        String[] comWords = commonWords.toArray(new String[commonWords.size()]);

        System.out.println("Total Number of Common Words are: "+comWords.length);
        for (int i = 0; i < comWords.length; i++) {
            System.out.println(i + 1 + ": " + comWords[i]);
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

它的出局是,

Total Number of Common Words are: 7
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
BUILD SUCCESSFUL (total time: 0 seconds)

当我运行下面几乎与上面相同的程序代码时使用相同的文件:

//Counting the number of matched words
public int getCommonWords(File File1, File File2) throws IOException {
    try {

        Scanner sc1 = new Scanner(new FileInputStream(File1));
        Scanner sc2 = new Scanner(new FileInputStream(File1));
        List<String> File1Content = new ArrayList<String>();
        List<String> File2Content = new ArrayList<String>();
        List<String> commonWords = new ArrayList<String>();

        //Getting the contents of File1
        while (sc1.hasNext()) {
            File1Content.add(sc1.next());
        }
        String arr1[] = File1Content.toArray(new String[File1Content.size()]);

        //Getting the contents of File1
        while (sc2.hasNext()) {
            File2Content.add(sc2.next());
        }
        String arr2[] = File2Content.toArray(new String[File2Content.size()]);

        int count = 0;

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    commonWords.add(arr1[i]);
                    count++;
                }
            }
        }

        String[] comWords = commonWords.toArray(new String[commonWords.size()]);

        System.out.println("Total Number of Common Words are: " + comWords.length);
        for (int i = 0; i < comWords.length; i++) {
            System.out.println(i + 1 + ": " + comWords[i]);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

它在输出区域显示以下内容:

run:
Total words of File1.txt are: 11
Total Number of Common Words are: 11
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
8: a
9: b
10: c
11: d

它甚至还显示了无与伦比的单词,为什么会这样呢? 请帮忙。

2 个答案:

答案 0 :(得分:3)

由于一个明显的原因,你得到空指针异常,变量commonWord没有指向数组(你没有为存储数组字符串分配内存)。

String commonWords[]=null;

你有2个解决方案:

  • 使用固定内存分配:

    String commonWords[]= new String[Math.min(arr1.length, arr2.length)];
    int commonCount = 0;
    ....
    commonWords[commonCount++] = arr2[i]
    
  • 使用动态内存分配:

    ArrayList<String> commonWords = new ArrayList<>();
    ...
    commonWords.add(arr2[i]);
    

答案 1 :(得分:2)

如果要保留逻辑和数据结构,请使用以下内容:

String commonWords[] = new String [Math.min(arr1.length, arr2.length)];

它远非完美,但允许您为数组指定值。

然后打印:

for(String str : commonWords) {
    if (str != null) {
         System.out.println(str);
    }
}