比较 Java 中的字符串数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41982868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:16:46  来源:igfitidea点击:

Compare string array values in Java

javaarraysstringcompareequals

提问by Jescanellas

I'm trying to compare each value from a string array with all the values of the same array to see if they are equal (something like "wordA" equals "wordA"). This is what I have:

我试图将字符串数组中的每个值与同一数组的所有值进行比较,以查看它们是否相等(类似于“wordA”等于“wordA”)。这就是我所拥有的:

FileReader input; 

input = new FileReader("file.txt"); 

BufferedReader reader = new BufferedReader(input); 

String line = reader.readLine(); 

String[] text = line.replaceAll("[^a-zA-Z ]", "").split("\s+"); 

for (int A = 0; A < text.length; A++) {

    for (int B = text.length; B > 0; B++){

        if (text[A].equals(text[B])){ 

        System.out.println("Repeated Word: "+text[A]);  

        }

    } 
} 

It's only comparing array spaces, so if A and B = 3 (for example), it will always be true, ignoring the string inside this space. So I'm getting as output all the words in the text.

它只是比较数组空间,因此如果 A 和 B = 3(例如),它将始终为真,忽略此空间内的字符串。所以我将文本中的所有单词作为输出。

回答by Janar

The second for-loop is faulty, you will get an array index out of bounds.

第二个 for 循环有问题,你会得到一个数组索引越界。

Instead you should start from the beginning too and exclude if index is equal

相反,您也应该从头开始并排除索引是否相等

String[] text = { "a", "b", "c", "d", "a" };
for (int a = 0; a < text.length; a++) {
  for (int b = 0; b < text.length; b++) {
    if (text[a].equals(text[b]) && a != b) {
      System.out.println("Reapeated word : " + text[a]);
    }
  }
}

or as was suggested in comments, start from the next element in array (in which case you don't have to check if the indices are equal)

或者如评论中所建议的,从数组中的下一个元素开始(在这种情况下,您不必检查索引是否相等)

String[] text = { "a", "b", "c", "d", "a" };
for (int a = 0; a < text.length; a++) {
  for (int b = a + 1; b < text.length; b++) {
    if (text[a].equals(text[b])) {
      System.out.println("Reapeated word : " + text[a]);
    }
  }
}

回答by Jeremy

It looks like that nested loop should be B-- instead of B++. Wouldn't that be a never ending loop? Either way, I don't think there is a reason to iterate in opposite directions in each loop. But to each their own.

看起来嵌套循环应该是 B-- 而不是 B++。那不会是一个永无止境的循环吗?无论哪种方式,我都认为没有理由在每个循环中以相反的方向进行迭代。但每个人都有自己的。

Also, there should be some sort of comparison to make sure A != B, otherwise you would be comparing the same value in the array. if (text[A].equals(text[B]))will at some point compare if (text[0].equals(text[0])), if (text[1].equals(text[1])), etc.

此外,应该进行某种比较以确保 A != B,否则您将比较数组中的相同值。if (text[A].equals(text[B]))将在比较某一时刻if (text[0].equals(text[0]))if (text[1].equals(text[1]))等等。

This would always return true at least once for each element in the array.

对于数组中的每个元素,这将始终至少返回一次 true。

You need to add a comparison of if (A != B)to fix it.

您需要添加一个比较if (A != B)来修复它。

Like so:

像这样:

for (int A = 0; A < text.length; A++) {
  for (int B = 0; B < text.length; B++) {
    if (A != B && text[A].equals(text[B])) {
         System.out.println("Repeated word:" + text[A]);
    }
  }
}