java - Copy 2D array within object by reference -
object1.java:
public class object1 { public double[][] var1; ... }
object2.java:
public class object2 { public double[][] var2; ... }
i want copy reference (shallow copy) object2.var2 object1.var1. here's i'm trying, isn't working:
object1 object1 = new object1(); object1.var1 = new double[2][]; system.arraycopy(object2.var2, 0, object1.var1, 0, object2.var2.length);
anyone know i'm going wrong? i'm getting java.lang.nullpointerexception
compile error. note object2.var2
populated data.
update 1:
note object2.var2 nx2 matrix, looks like:
object2.var2[0][0]=1.232 object2.var2[0][1]=23.233 object2.var2[1][0]=3.23 object2.var2[1][1]=32.12 ... object2.var2[n][0]=3.23 object2.var2[n][1]=32.12
i see java.lang.nullpointerexception
when try following:
object1.var1=new double[object2.var2.length][2]; object1.var1=object2.var2;
object2 object2 = new object2(); // need iniialize ur object2.var2 first object2.var2 = new double[][]{ {1.232, 23.233},{3.23,32.12},{3.23,32.12}}; //or object2.var2 = new double[3][2]; object2.var2[0][0]=1.232; object2.var2[0][1]=23.233; object2.var2[1][0]=3.23; object2.var2[1][1]=32.12; object2.var2[2][0]=3.23; object2.var2[2][1]=32.12; system.out.println(object2.var2); object1 object1 = new object1(); object1.var1= new double[object2.var2.length][2]; system.out.println(object1.var1); system.arraycopy(object2.var2, 0, object1.var1, 0, object2.var2.length);
Comments
Post a Comment