让它成为矩阵的定义:
private List<<List<Object>> lines;
public boolean equals(Object o){
if(this==o)
return true;
if((o==null) || (this.getClass()!=o.getClass()))
return false;
Matrix m = (Matrix) o;
return ????;
}
我如何比较所有这些行和列。
我可以用吗
return this.lines.equals(o.getLines());
??
谢谢
最佳答案
或多或少,尽管确切的代码并不完全有效。
来自 List.equals
javadoc:
Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).)
你确实可以使用这个方法来判断是否相等,但是你需要先转换o
。这是一个安全的转换,因为您已经检查了该类(尽管您可能会考虑改用 instanceof Matrix
)。
return this.lines.equals(((Matrix)o).getLines());
关于java - 列表列表上的等于方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443342/