dcsimg
Collections to array
6 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pothios_Machera
Posted On:   Wednesday, March 31, 2004 06:23 AM

I am wondering how can i convert collections to array using toArray?

my sample code:


Collections c = user.getNames();
//user method returns a collection.

how can i change my retrieved collection? something like this?

Sting [] list=c.toArray ??????

Re: Collections to array

Posted By:   Francis_Wong  
Posted On:   Wednesday, April 7, 2004 02:07 PM



Pathios - Try this code snippet:

--------------------

Collection c = new ArrayList();

c.add("John Smith");

c.add("Mary Smith");


// The next line will cause an ArrayStoreException

// c.add(new Integer(5));


// The next line will cause a ClassCastException

// String[] myList = (String [])c.toArray();



String[] myList = (String [])c.toArray(new String[c.size()]);


for (int i=0; i < myList.length; i++) {

     System.out.println(myList[i]);

}

----------------------


It seems like this is overkill, and the following line should have worked just
fine:


    String[] myList = (String [])c.toArray()


But Java doesn't allow an Object[] array to be cast as a String[]
because there's no guarantee that every item in the array is of the same type
(String in this example).


 




Re: Collections to array

Posted By:   Bhaskar_Raja  
Posted On:   Tuesday, April 6, 2004 05:59 AM

use like this.
1. Once data into collection

Collect c;
Object a[] =c.toArray;
do casting with string.

like
String arr[] = new String[a.length];
for ( int i = 0; i < a.length; i++)
{
arr[i] = a[i];
}

If any casting problem use a[i].toString();
Now you can get all the data into your String []list;.

Re: Collections to array

Posted By:   Bhaskar_Raja  
Posted On:   Tuesday, April 6, 2004 05:59 AM

use like this.
1. Once data into collection

Collect c;
Object a[] =c.toArray;
do casting with string.

like
String arr[] = new String[a.length];
for ( int i = 0; i < a.length; i++)
{
arr[i] = a[i];
}

If any casting problem use a[i].toString();
Now you can get all the data into your String []list;.

Re: Collections to array

Posted By:   Pothios_Machera  
Posted On:   Wednesday, March 31, 2004 11:53 PM

Thanks for the info man guys

Re: Collections to array

Posted By:   Glenn_Mason  
Posted On:   Wednesday, March 31, 2004 11:41 PM

There are two toArray methods -- one returns an Object array, the other one attempts to return an array of the type you pass it.



For your example,




Collections c = user.getNames();
String [] list = c.toArray(new String[]{});


The new String[]{} is a shortcut for a new, empty String array.

Re: Collections to array

Posted By:   Tim_Frith  
Posted On:   Wednesday, March 31, 2004 10:40 PM

I'm assuming you mean Collection and not Collections (which is a static class of utility methods that operate on Collection)...

Collection.toArray() returns an array of Objects. If you want Strings you would need to cast each element to String as you pull it out of the object array, something like this...

Collection c = user.getNames();
Object[] obj = c.toArray();
int i;
String[] str = new String[obj.length];
for(i=0; i < obj.length; i++) {
str[i] = (String) obj[i];
}


Probably more efficient to skip the object array altogether, something like this...

Collection c = user.getNames();
Iterator it = c.iterator();
String[] str = new String[c.size()];
int i = 0;
while(it.hasNext()) {
str[i] = (String) it.next();
i++;
}
About | Sitemap | Contact