Reputation: 6187
how to copy some of a object array to another object array
i have a class like this
class sd{
String a="";
String b="";
String c="";
String d="";
int lenph=12;
boolean s=false;
}
and two array like this
sd[] stexp=new sd[12];
sd[] st=new sd[3];
i want to copy 3 of stexp to st how can i do that?
i do this but its not working
sd[] stexp=new sd[12];
for(int c=0;c<stexp[0].lenph;c++){
stexp[c]=new sd();
}
sd[] st=new sd[3];
for(int c=0;c<3;c++){
st[c]=new sd();
}
for(int i=0;i<12;i++){
stexp[i].a="f"+i;
stexp[i].b="f"+i;
stexp[i].c="f"+i;
stexp[i].d="f"+i;
}
for(int i=0;i<3;i++){
st[i].a=stexp[i].a;
st[i].b=stexp[i].b;
st[i].c=stexp[i].c;
st[i].d=stexp[i].d;
}
b+=st[0].a+"\n";
b+=st[0].b+"\n";
b+=st[0].c+"\n";
b+=st[0].d+"\n";
sho.setText("b="+b);
thanks for any help. :)
opps i changed wrong codes .
i want to copy a object array to another object array and i do with
System.arraycopy(stexp, 0,st , 0, 1);
but when i running the codes in eclipse its not working.
Upvotes: 0
Views: 143
Reputation: 1
I would double-check the field names (if this is actually the code you are trying to run). Your class sd does not include the member variables you are trying to write/read: deg, var, mult, power. It has a, b, c, and d.
You might also have a typo in the first for-loop condition ("lengph").
Finally, keep in mind that this is only copying over the values of member variables of those 3 stexp objects. That is not the same as copying the objects, which might be what you want.
Upvotes: 0
Reputation: 2506
check this example.use arraycopy
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
Upvotes: 1