0

I want to create a list that contains linked list of MyObject.

LinkedList<MyObject>[] list  = new LinkedList<MyObject>[n];

But it shows:

Main.java:19: error: generic array creation LinkedList<MyObject>[] list  = new LinkedList<MyObject>[n];

How can I create this kind of list?

5
  • Do you mean "a array of linked lists" or do you mean "a list or linked lists". For the second case see my answer (after all you asked for it), for the first case your code looks fine for older Java. Commented Oct 21, 2014 at 0:59
  • @afzalex The angle bracketed expressions (<...>) were hidden by the way posts are formatted. The editor did not add them, they appeared once the code was spaced in. Commented Oct 21, 2014 at 1:04
  • Oh! that is why I was thinking there is no problem here. But I am wondering how the answerers knew that, and started to answer. @Radiodef Commented Oct 21, 2014 at 1:07
  • @afzalex They clicked 'edit' and saw it or they guessed. Commented Oct 21, 2014 at 1:08
  • Or maybe we were drunk at the time. Commented Oct 21, 2014 at 1:10

2 Answers 2

1

Why are you trying to create an array of LinkedLists? it is after all already a List.

Change your code to

List <MyObject> list = new LinkedList<MyObject>();

if you want a collection of these LinkedLists then I suggest that you create a new List

List <LinkedList<MyObject>> theBigList = new ArrayList <> ();

and then you can add to this list

theBigList.add (list); 
Sign up to request clarification or add additional context in comments.

5 Comments

@SleimanJneidi Thanks. If there is a good reason for a DV, I would really like to know about it so I can add value to this site.
Before posting answer, have you checked that the problem is there? his code was working and not given any error for me. Now somebody has edited his question.
Well, the question is edited now to a completely different question, maybe thats why, the DV was a better mind reader!
Anyway, I removed my DV before getting too late. apologize. but I thought we should atleast first check if the OP was correct or not.
Although I want to know if the last unedited code was working for you, with some other version of java, I tried 7 and 8.
1

A List of Lists would look like:

List<List<MyObject>> listList = new LinkedList<>();
List<MyObject> list1 = new LinkedList<>();
List<MyObject> list2 = new LinkedList<>();
listList.add(list1);
listList.add(list2);
List<MyObject> list3 = listList.get(0);

And an Array of LinkedLists would look like:

@SuppressWarnings("unchecked")
List<MyObject>[] listArray = new LinkedList[n];
List<MyObject> list1 = new LinkedList<>();
List<MyObject> list2 = new LinkedList<>();
listArray[0] = list1;
listArray[1] = list2;
List<MyObject> list3 = listArray[0];

Both assume the diamond operator (<>) which is a Java 7 shortcut, but can be replaced with the full type (new LinkedList<MyObject> for the second and LinkedList<List<MyObject>> for the first case).

And the fact that arrays cannot be instantiated of generic type (due to all kinds of weird internals). See for example http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5105887

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.