I have a class that implements a 2Dtable. The elements in the table is generic. And the table is stored like this:
public Object[][] list;
The problem is that calling clone on this apparently doesn't work. Note that my testcase initializes the table to store normal Integers.
Tabell2D<Integer> en = new Tabell2D<Integer>(5,5);
en.sett(0, 0, 55);
Tabell2D<Integer> to = en.clone();
to.sett(0, 0, 11);
assertTrue(en.equals(to));
Here I make a table. Change it, clone it. Change the clone, and compare them. Obviously the clone is changed. Even so, this assertTrue is well true.
The equal method is generated by eclipse:
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tabell2D other = (Tabell2D) obj;
if (bredde == null) {
if (other.bredde != null)
return false;
} else if (!bredde.equals(other.bredde))
return false;
if (høyde == null) {
if (other.høyde != null)
return false;
} else if (!høyde.equals(other.høyde))
return false;
if (!Arrays.equals(liste, other.liste))
return false;
return true;
}
I assume that the problem is either in the compare of the list variable in equal, or the problem is in the clone method. Clone method:
public Tabell2D<E> clone(){
Tabell2D<E> nyTab = new Tabell2D<E>(this.bredde,this.bredde);
nyTab.liste = liste.clone();
return nyTab;
}