简体   繁体   English

使用Java中的boolean方法将一个ArrayList与另一个ArrayList进行比较

[英]Comparing one ArrayList to another ArrayList using boolean method in java

I am trying to get a boolean method to return true or false on wether two arrayLists are equal to each other. 我试图得到一个布尔方法,以使两个arrayLists彼此相等时返回true或false。 The arraysLists are array and array1 . arraysListsarrayarray1 The user inputs them. 用户输入它们。 Right now here is the code that I thought would work: 现在,这里是我认为可以运行的代码:

public boolean equals(){
//if both are equal return true, else false
boolean test = false;
for(int i = 0; i < array1.size() && !test; i++){
    if(array1.get(i) == (array.get(i))){
        test = true;
    }       
}
return test;
  }

except even when all the arrayLists numbers match the other arrayLists numbers, it returns false . 即使所有arrayLists数字都与其他arrayLists数字匹配,它arrayLists返回false

You don't need to overwrite the equals method, as there is one already provided for lists that does exactly what you need. 您无需覆盖equals方法,因为已经为列表提供了一种完全符合您需要的方法。

If you insist of writing it yourself there is a simple error in your code. 如果您坚持要自己编写,则代码中会出现一个简单的错误。 Because you initialize test to be false, "&& !test" lets your loop exist right at the start. 因为您将test初始化为false,所以“ &&!test”让您的循环从一开始就存在。

The correct version would be: 正确的版本是:

public boolean equals(){
if(array.size()!=array1.size) return false; // test for different length
for(int i = 0; i < array1.size(); i++){
    if(!array1.get(i).equals(array.get(i))){
        return false;
    }       
 }
 return true;
}

double equals (==) is dangerous. 双重等于(==)很危险。 You are actually returning objects in your code up there, so you should definitely use equals() instead 您实际上是在代码中返回对象,因此您绝对应该使用equals()代替

Think that you are only iterating one array. 认为您只迭代一个数组。 Think what can go wrong there. 想想那里可能出什么问题。 Also take a look at your control statement. 还要看看您的控制语句。

If you carefully follow the flux of your code you quickly will realize why is false. 如果您仔细地遵循代码的流程,您很快就会意识到为什么是错误的。

You'll need to change your code to this: 您需要将代码更改为此:

public boolean equals(){
    if (array1.size() != array.size()) return false;

    for(int i = 0; i < array1.size(); i++){
        if(!array1.get(i).equals(array.get(i))){
            return false;
        }       
    }

    return true;
}

First off, you have to start with test being true and return false if you find something that isn't equal, because this clearly shows that the ArrayLists are not equal. 首先,您必须从test为true开始,如果发现不相等的内容,则返回false,因为这清楚地表明ArrayLists不相等。 You actually don't need the test variable at all, so I took it out. 实际上,您根本不需要test变量,因此我将其删除。 Just return false if you find something that isn't equal. 如果发现不相等的内容,则返回false。 If you don't find something that isn't equal, it will never return false and will just return true at the end. 如果找不到不相等的值,则它将永远不会返回false,最后只会返回true。 Second, you have to use the equals() method, because ArrayLists use the Integer class, not the int primitive so == will check if they are the same object, not if they are the same number. 其次,您必须使用equals()方法,因为ArrayLists使用Integer类,而不是int原语,所以==将检查它们是否是同一对象,而不是它们是否是相同的数字。 Lastly, to deal with comparing arrays of different sizes, you should compare their size and return false if they are not the same size, since there is no way they can be equal. 最后,要处理比较不同大小的数组,您应该比较它们的大小,如果它们的大小不同,则返回false,因为它们不可能相等。

You should just 'reverse' your method. 您应该只是“反转”您的方法。 Assume the arrays are equal first. 假设数组首先相等。 It should then check on each iteration if the element differs. 然后,应在每次迭代中检查元素是否不同。 If the element differs, then set a "not equal" flag. 如果元素不同,则设置一个“不相等”标志。 In pseudo-codee 用伪编码

boolean different = false;
for (each element of array 1) {
    if (element != element of array 2) different = true
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM