Discussion on Java at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
Posts: 19
Joined: Mon Nov 21, 2011 10:49 am
Location: UPR, Cuba
Gender: None specified


Postby adrianc » Tue Feb 14, 2012 11:47 am

Hello i need to know if there is a way to convert from a generic data structure (say List<T>, Vector<T>, etc) to an array of T, because the method .toArray() returns an Object[]... and i need a T[]...
any words???

Posts: 6
Joined: Tue Jan 24, 2012 12:38 am
Gender: None specified

Re: Arrays

Postby gmo » Fri May 11, 2012 1:41 am

try this:
Object[] array = (Object[]) arrayList.toArray(new Object[0]);

i.e for int's
Integer[] a = (Integer[]) arrayList.toArray(new Integer[0]);

you have to use class name instead of data type. works for custom classes too. :D

Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest