im trying to copy the contents of one array to another without pointing to the same memory, but i cant.
My Code:
class cPrueba {
private float fvalor;
public float getFvalor() {
return fvalor;
}
public void setFvalor(float fvalor) {
this.fvalor = fvalor;
}
}
List<cPrueba> tListaPrueba = new ArrayList<cPrueba>();
List<cPrueba> tListaPrueba2 = new ArrayList<cPrueba>();
cPrueba tPrueba = new cPrueba();
tPrueba.setFvalor(50);
tListaPrueba.add(tPrueba);
tListaPrueba2.addAll(tListaPrueba);
tListaPrueba2.get(0).setFvalor(100);
System.out.println(tListaPrueba.get(0).getFvalor());
The result is "100.0" ....
Still pointing to the same object... Any short way to copy ? (without for(..){})
EDIT:
class cPrueba implements Cloneable {
private float fvalor;
public float getFvalor() {
return fvalor;
}
public void setFvalor(float fvalor) {
this.fvalor = fvalor;
}
public cPrueba clone() {
return this.clone();
}
}
List<cPrueba> tListaPrueba = new ArrayList<cPrueba>();
List<cPrueba> tListaPrueba2 = new ArrayList<cPrueba>();
cPrueba tPrueba = new cPrueba();
tPrueba.setFvalor(50);
tListaPrueba.add(tPrueba);
for ( cPrueba cp : tListaPrueba )
tListaPrueba2.add(cp);
tListaPrueba2.get(0).setFvalor(100);
System.out.println(tListaPrueba.get(0).getFvalor());
Still get 100...
ArrayList?