Creating an Array of LinkedLists in Java
In Java, questions arise when attempting to create an array of LinkedLists. When declaring an array like private LinkedList
This raises two issues:
It's important to note that IntegerNode is a user-defined class in this scenario.
Resolution
The solution to this issue is to cast the type in the declaration to allow the array creation. The revised declaration should be:
myMatrix = (LinkedList<IntegerNode>[]) new LinkedList<?>[numRows];
Explanation
In Java, generic arrays are not directly supported. Instead, raw types (i.e., types without type parameters) are used. Casting the type allows the compiler to infer the correct generic type for the array.
So, the type LinkedList
The above is the detailed content of Why Can\'t I Create an Array of LinkedLists in Java and How Do I Fix It?. For more information, please follow other related articles on the PHP Chinese website!