Image

Arrays as linked lists revisited (with generics)

Awhile ago, I posted about converting an array to a linked list during construction. Here is an answer someone was generous enough to provide:

given an existing array called arr, use:
new LinkedList(Arrays.asList(arr));

I'm having a problem with this notation because of generics. What is happening is that I have an existing int array:
int [] origArr = new int[LIMIT]
which eventually gets filled with many entries.
When I try to use the above code, like:
return new LinkedList(Arrays.asList(origArr));

I get the errors:
"the expression of type LinkedList needs unchecked conversion to conform to LinkedList<Integer> / The constructor LinkedList(Collection) belongs to the raw type LinkedList. References to generic types LinkedList<E> should be parameterized."

(It might be relevant: The function I'm writing has the following signature:
static LinkedList<Integer> testFunction(int [] origArr) {...})

Based on the errors given, I tried the following modifications. All of them continue to give one error or another:
return new LinkedList <Integer> (Arrays.asList(origArr));    // "The constructor LinkedList<Integer>(List <int[]>) is undefined."
return new LinkedList <Integer>(Arrays.asList<Integer>(origArr))// "Arrays.asList cannot be resolved.  Integer cannot be resolved."
return (return new LinkedList<Integer>(new LinkedList(Arrays.asList(origArr)));  // "type safety..."

I'm wondering if anyone knows how to do this correctly?

Of course, for all this trouble, it's just way easier to write a for loop to do the task:
LinkedList<Integer> myLinkedList = new LinkedList<Integer>();
for (int i=0; i<origArr.length; i++)
   myLinkedList.add(origArr[i]);
return myLinkedList;


And this works fine! In other words, I'm not asking the question in this post because I need to know for functionality - I have another pretty simple way to do the same thing. But now I'm curious and want to know how to use generics correctly. :-).