[英]can not compare two arraylist object
import java.util.ArrayList;
import java.util.List;
public class arr {
public arr () {
// creat empty list
List<Integer> alpha = new ArrayList<>();
}
public static void main (String []args ) {
arr com1 = new arr();
arr com2= new arr();
//returning false but should be true the two are empty array
com1.equals(com2);
}
}
I am trying to create a class that can build a an empty array but when I am trying to compare the two-object its returning false, but the two have an empty arraylist so it should return true我正在尝试创建一个 class 可以构建一个空数组但是当我尝试比较两个对象时它返回 false,但是两者有一个空 arraylist 所以它应该返回 true
You are trying to call a missing method called isEmpty()
.您正在尝试调用一个名为isEmpty()
的缺失方法。
You also need to move the list alpha
as a field for the class, so that it can be accessed by the isEmpty()
method.您还需要将列表alpha
作为 class 的字段移动,以便可以通过isEmpty()
方法访问它。
To check for equality, just overload the equals()
method.要检查是否相等,只需重载equals()
方法。
Also as @Billy mentioned, you should override the hashCode too (so that it'll still work if you use hashSet/hashMap to store the arr class later on in your codes)同样正如@Billy 提到的,您也应该覆盖 hashCode(这样,如果您稍后在代码中使用 hashSet/hashMap 来存储 arr class ,它仍然可以工作)
import java.util.ArrayList;
import java.util.List;
public class arr {
List<Integer> alpha = new ArrayList<>();
//creates the isEmpty method
boolean isEmpty() {return alpha.isEmpty();}
//check if two arr classes are the same
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
arr other = (arr) obj;
//checks if the alpha of this is the same as the alpha in obj
return alpha.equals(other.alpha);
}
//also override the hashCode method so that it'll work correctly for hash containers like hashMap and hashSet.
@Override
public int hashCode(){ return alpha==null?0: alpha.hashCode(); }
public static void main (String []args ) {
arr com1 = new arr();
arr com2= new arr();
//calling the isEmpty() method from the main.
System.out.println("com1 Empty? =" +com1.isEmpty());
//checks if com1 and com2 are equal
System.out.println("com1 same as com2? =" +com1.equals(com2));
}
}
You can use size() method to check Arraylist size.您可以使用 size() 方法检查 Arraylist 大小。 Checkout this for further reference: https://www.geeksforgeeks.org/arraylist-size-method-in-java-with-examples/查看此内容以供进一步参考: https://www.geeksforgeeks.org/arraylist-size-method-in-java-with-examples/
Your code is syntactically wrong.您的代码在语法上是错误的。
class arr class arr
arr() is a method, so: arr() 是一种方法,因此:
Naming still messy, but now at lest you can see it compiling (I hope)命名仍然很乱,但现在至少你可以看到它编译(我希望)
import java.util.ArrayList;
import java.util.List;
public class ArrCreator
{
public List<Integer> createEmptyList()
{
// create empty list
List<Integer> alpha = new ArrayList<>();
return alpha;
}
public static void main(String[] args)
{
ArrCreator creator = new ArrCreator();
List<Integer> com1 = creator.createEmptyList();
List<Integer> com2 = creator.createEmptyList();
System.out.println(com1.equals(com2));
}
}
output: true output:真
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.