使用string.equals比较java中的两个字符串数组(Comparing two string arrays in java using string.equals)

编程入门 行业动态 更新时间:2024-10-27 22:21:01
使用string.equals比较java中的两个字符串数组(Comparing two string arrays in java using string.equals)

我有一个工作正常的方法,并给出了期望的结果。 这是它的代码:

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

它甚至还展示了无与伦比的单词,为什么如此呢? 请帮忙。

I have a method that works fine and gives desired result. Here is it's code:

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

}

it's out put is,

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)

Using the same files when I run the program code below almost same as above:

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

it displays following in the output area:

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

it is even displaying the unmatched words as well, why is it so? Please help.

最满意答案

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

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

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

然后打印:

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

If you want to preserve your logic and data structures, then use something like:

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

It's far from perfect, but will allow you to assign values to your array.

Then to print:

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

更多推荐

本文发布于:2023-08-06 18:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   两个   equals   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!