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.
Post Reply
Posts: 19
Joined: 8 years ago
Location: UPR, Cuba
Gender: None specified


Post by adrianc » 7 years ago

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: 7 years ago
Gender: None specified

Re: Arrays

Post by gmo » 7 years ago

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

Post Reply

Return to “Java”