So for starters lets say that I have a LinkedList<String>
,
I can easily convert it to an array via toArray()
. i.e.
LinkedList<String> strList = new LinkedList<String>();
String[] strArray = strList.toArray(new String[0]);
But Lets say I have a LinkedList<T>
Then I the following code:
LinkedList<T> tList = new LinkedList<T>();
T[] strArray = tList.toArray(new T[0]);
I get the Cannot create a generic array of T error message.
How can I get around this?
Specifically in my class I have LinkedList<AbstractNode<T>> nodes
, and I am trying to implement a getAll() method that returns all the nodes as an Array.
Thanks!
Note Péter Török's answer provides the correct answer to my problem, but for me simply returning an ArrayList instead of [] as Bar mentioned, ended up smoothing my code out a lot.
Note2 after looking at my code a bit more i'm not even sure if any conversion was necessary to begin with, LinkedList was fine for what I was trying to do... :-/