메신저는 동일한 메모리를 가리 키지 않고 한 배열의 내용을 다른 배열로 복사하려고 시도하지만 위장 할 수 없습니다.배열을 다른 배열로 복사
내 코드 :
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());
결과는
아직도 동일한 개체를 가리키는 .... "100.0"입니다 ... 어떤 짧은 방법은 복사? (.. (대한 없음) {})편집 :
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());
스틸 (
당신은 모든 개체를 복제해야하고, 따라서이를 반복 할 수 있습니다. –
** 배열 ** 또는'ArrayList'? – Runemoro