我希望能够按顺序将一个字符串与所有其他字符串进行比较,然后向下移动到下一个字符串,并将该字符串与其下面的所有其他字符串进行比较。
这是我的代码。它正确比较第一个字符串,但没有找到匹配项。然而,当另一个 for 循环移动到 i 的下一个值时,每个字符串 =1,i 也是如此,因此它会比较自身。每次经过另一个循环时,如何才能将内部 for 循环移动一个值?
for (int everystring = 0; everystring < children.length; everystring++) {
String current = children[everystring].substring(0,
children[everystring].indexOf("_"));
for (int i = 1; i < children.length; i++) {
String innercurrent = children[i].substring(0,
children[i].indexOf("_"));
if (current.equalsIgnoreCase(innercurrent)) {
System.out.println("Match");
} else
System.out.println("No Match");
}
System.out.println();
}
最佳答案
始终从 0 到 children.length
循环[在内循环中],并在内循环的开头添加以下条件:
if (i == everything) continue;
它将跳过内部循环的每次迭代,其中 i == everything
,因此您将只检查不相等的字符串。
但请注意,您将检查每 2 个字符串两次(例如:您将检查 i == 1
、everything == 2
并且i == 2,一切== 1
如果你不想要它:在内部循环中迭代,从一切+ 1
直到children.length
关于java - 按顺序比较字符串与其他字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9811797/