java 用for循环比较java中的两个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26857776/
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
Comparing two arrays in java with for loop
提问by user3376064
I am having difficulties with this program. I have to compare the two arrays by reading the arrays from the console and after the user enters them, print a statement for whether or not they are true. I am not sure if I can use the compare function, but I have to do it with a for loop.
我在使用这个程序时遇到了困难。我必须通过从控制台读取数组来比较这两个数组,并在用户输入它们后,打印一个语句以确定它们是否为真。我不确定是否可以使用 compare 函数,但我必须使用 for 循环来完成。
Here is what I have tried:
这是我尝试过的:
import java.util.Scanner;
@SuppressWarnings("unused")
public class TwoArrays {
@SuppressWarnings("unused")
public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
System.out.println("enter the first array");
String firstArrayAsString = input1.nextLine();
System.out.println("enter the second array");
String secondArrayAsString = input1.nextLine();
if (firstArrayAsString. length() != secondArrayAsString.length()){
System.out.println("false.arrays are not equal");
} else {
int arrayLen = firstArrayAsString.length();
char[] firstArray = firstArrayAsString.toCharArray();
char[] secondArray = secondArrayAsString.toCharArray();
int i = 0;
while (i < arrayLen && firstArray[i] == secondArray[i]); {
i++;
}
if (i == arrayLen) {
System.out.println("true.they are equal");
} else {
System.out.println("False.they are not equal");
}
}
input1.close();
}
}
采纳答案by Naman Gala
Try this code.
试试这个代码。
char[] firstArray = {'a', 'b', 'c'};
char[] secondArray = {'a', 'b', 'c'};
if (firstArray.length != secondArray.length) {
System.out.println("False.they are not equal");
} else {
boolean isEqual = true;
for (int i = 0; i < firstArray.length; i++) {
if (firstArray[i] != secondArray[i]) {
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if (isEqual)
System.out.println("true.they are equal");
}
回答by AdamMc331
I would change this to use the charAt(int)
function rather than changing the values to an array. Since you're reading them as strings, just compare them as strings, like this:
我会将其更改为使用该charAt(int)
函数,而不是将值更改为数组。由于您将它们作为字符串读取,只需将它们作为字符串进行比较,如下所示:
if(firstArrayAsString.length() != secondArrayAsString.length()){
return false;
} else{
for(int i = 0; i < firstArrayAsString.length(); i++){
if(firstArrayAsString.charAt(i) != secondArrayAsString.charAt(i){
return false;
}
}
return true;
}
This first checks if the arrays are the same size. If they are not, we can already say they are not equal. If they are the same size, loop through and make sure the character at each index matches. If at any point we find characters that don't match, we know it is not equal. If we loop through the entire thing without finding a difference, then the two arrays are equal.
这首先检查数组的大小是否相同。如果不是,我们已经可以说它们不相等。如果它们的大小相同,则循环并确保每个索引处的字符匹配。如果在任何时候我们发现不匹配的字符,我们就知道它不相等。如果我们遍历整个事物而没有发现差异,那么这两个数组是相等的。
I would like to add, though, that if it's not required to use a for loop, why not just use:
不过,我想补充一点,如果不需要使用 for 循环,为什么不使用:
firstArrayAsString.equals(secondArrayAsString);
to compare the two string values?
比较两个字符串值?
Minor Edit
次要编辑
Rereading your question I see that you said you can't use the equals method, but I am choosing to leave this in my answer as a possible alternative for future readers.
重新阅读您的问题,我看到您说您不能使用 equals 方法,但我选择将其保留在我的答案中,作为未来读者的可能替代方案。
回答by Ibrahim Elsobkey
BufferedReader existFile = new BufferedReader(
new FileReader("D:/1.txt"));
BufferedReader recievedFile = new BufferedReader(
new FileReader("D:/2.txt"));
String str, str1 = null;
List<String> list = new ArrayList<String>();
List<String> list1 = new ArrayList<String>();
while ((str = existFile.readLine()) != null) {
list.add(str);
}
while ((str1 = recievedFile.readLine()) != null) {
list1.add(str1);
}
String[] stringArr = list.toArray(new String[list.size()]);
String[] stringArr1 = list1.toArray(new String[list1.size()]);
for (int i = 0; i < stringArr1.length; i++) {
for (int j = 0; j < stringArr.length; j++) {
if (stringArr1[i].equalsIgnoreCase(stringArr[j])) {
System.out.println(stringArr1[i]);
}
}
}
回答by Fuuka Adachi
boolean isEqual = true;
for(int i=0;i<arrayLen;i++){
if(firstArray[i] != secondArray[i]){
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if(isEqual){
System.out.println("true.they are equal");
}
If that not work, try to change (firstArray[i] != secondArray[i]) to (!(firstArray[i].equals(secondArray[i]))) or st like that.
如果这不起作用,请尝试将 (firstArray[i] != secondArray[i]) 更改为 (!(firstArray[i].equals(secondArray[i]))) 或 st 之类的。
回答by Slim_user71169
int i = 0;
while (i < arrayLen && firstArray[i] == secondArray[i]); {
i++;
}
Change this to below:
将此更改为以下内容:
boolean isEqual = true;
for(int i=0;i<arrayLen;i++){
if(firstArray[i]!=secondArray[i]) {
isEqual = false;
break;
}
}
Finally, you can show result with flag 'isEqual'
最后,您可以使用标志“isEqual”显示结果
if(isEqual) System.out.println("Equal");
else System.out.println("Not Equal");
回答by Scary Wombat
Your problem is this code
你的问题是这个代码
while (i < arrayLen && firstArray[i] == secondArray[i]); {
has a semicolon at the end; // remove it
末尾有一个分号;// 去掉它
Plus as mentioned before
加上之前提到的
if (i == arrayLen) {
should be
应该
if (i == arrayLen - 1) {
Note
笔记
The warnings that you surpressed using
您使用的 surpressed 警告
@SuppressWarnings("unused")
was probably telling you something useful.
可能是在告诉你一些有用的东西。