1

I'm really confused. My code:

public void testList(){
    int cnt = 3;
    LinkedList<LvRow>[] vListItems = new LinkedList[cnt]; //eclipse suggest warnning
    for (int i = 0; i < cnt; i++) {
        vListItems[i] = new LinkedList<LvRow>();
    }
}

eclipse suggest a warning:

Type safety: The expression of type LinkedList[] needs unchecked conversion to conform to LinkedList<LvRow>[]

It seems a cast problem. I have tried many times, but I don't know how to cast. Anyone can help me?

By the way, if following codes is same, or have any diffence?

 LinkedList<LvRow>[] vListItems = new LinkedList[cnt];
 LinkedList<LvRow> vListItems[] = new LinkedList[cnt];
4
  • 2
    Not strictly speaking an answer to this question, but why can't you just use LinkedList<LvRow>[] vListItems = new LinkedList<LvRow>[cnt]; Commented Nov 12, 2013 at 10:08
  • @RichardTingle That does unfortunately not work because of a limitation in Java generics; you can't create an array of a type with a type parameter. Commented Nov 12, 2013 at 10:09
  • 5
    Don't mix collections and arrays! Choose one and stick with that. And the one to choose is almost always "collections" (except maybe for byte[]). Commented Nov 12, 2013 at 10:10
  • Once you find an acceptable answer you should accept it. N its not because I have answered your question. Just a nugget as you are a new user. stackoverflow.com/help/accepted-answer Commented Nov 12, 2013 at 14:31

3 Answers 3

7

Cannot Create Arrays of Parameterized Types

You cannot create arrays of parameterized types. For example, the following code does not compile:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

The following code illustrates what happens when different types are inserted into an array:

Object[] strings = new String[2];
strings[0] = "hi";   // OK
strings[1] = 100;    // An ArrayStoreException is thrown.

If you try the same thing with a generic list, there would be a problem:

Object[] stringLists = new List<String>[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.

If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException.

This all happens because of type erasure.

Solution

you should use List collection for this so that the compiler can do static type checking and provide you type safety.

List<LinkedList<LvRow>> list = new ArrayList<LinkedList<LvRow>>();
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but suggest a solution? (ArrayList<LinkedList<LvRow>>...)
1

When using generics you cannot create an array of objects with a generic types. This is because arrays perform an array store check which guarantees an object in the array is of the component type. Since type erasure removes type arguments the exact type of the array cannot be known at runtime. Read More

You can work around this limitation by creating a List<List<LvRow>>.

List<List<LvRow>>[] vListItems = new LinkedList<List<LvRow>>(); 

3 Comments

@Jesper Completely missed this question was in reference to arrays. I have corrected.
For other readers: Kevin edited his answer, so my comment above is no longer valid. (Reverted my -1, Kevin).
-1

You can use like this:

LinkedList<LvRow>[] vListItems = new LinkedList<LvRow>[cnt];

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.