Java Object Array item to String Array
Posted
by user341264
on Stack Overflow
See other posts from Stack Overflow
or by user341264
Published on 2010-05-14T13:40:59Z
Indexed on
2010/05/14
13:44 UTC
Read the original article
Hit count: 164
Say I have the following:
Class myclass
{
public string stra ="", strb = ""
myclass(String a, String b){stra=a;strb=b}
}
//then in the app I want to do:
myclass myclassinst1 = new myclass("blah","xxxx");
myclass myclassinst2 = new myclass("blah2","yyyy");
myclass myclassinst3 = new myclass("blah3","zzzz");
list <myclass> mylist = new ArrayList<myclass>();
mylist.add(myclassinst1 );
mylist.add(myclassinst2 );
mylist.add(myclassinst3 );
//How would I then convert that to a String[] Array of all the stra elements without using a loop.
//eg:
String[] strarr_a = mylist.toarray(myclass.stra);
String[] strarr_b = mylist.toarray(myclass.strb);
//instead of having to do
String[] strarr_a = new String[mylist.size()];
String[] strarr_b = new String[mylist.size()];
for (int i=0;i<mylist.size();i++)
{
strarr_a[i] = mylist.get(i).stra;
strarr_b[i] = mylist.get(i).strb;
}
© Stack Overflow or respective owner