0

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.

2
  • 2
    Which 3 from stexp do you want to copy into st? Do you want to sort stexp first and then transfer 3 over to st? Commented Feb 26, 2013 at 18:37
  • thanks for your attention i repair my question i hope u see that again. Commented Feb 27, 2013 at 6:11

2 Answers 2

1
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));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but my array is objective array and its nut working whit System.arraycopy()
0

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.

1 Comment

thanks for your attention i repair my question i hope u see that again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.