Java is an object-oriented language, so you should prefer to model the problem using the appropriate class, rather than using a primitive array directly. This makes, for example, object equality comparisons easier by encapsulating the logic in equals()
. You cannot override equals()
for primitive arrays.
So you can define
public class ArrayCell {
private final long[] content;
public ArrayCell(long[] content) {
this.content = content;
}
@Override
public boolean equals(Object another) {
if (another == null || another.getClass() != this.getClass()) {
return false;
}
return Arrays.equals(this.content, another.content);
}
@Override
public int hashCode() {
return Objects.hash(content);
}
}
And the client code will look like this:
List<ArrayCell> cells = new ArrayList<>() ;
cells.add(new ArrayCell(new long[]{1L, 0L}));
cells.add(new ArrayCell(new long[]{1L, 1L}));
cells.add(new ArrayCell(new long[]{1L, 2L}));
for (long r = 0L; r < 3; r++){
for (long c = 0L; c < 3; c++){
if(cells.contains(new ArrayCell(new long[]{r,c}))){
}
}
}
source
share