Generic array creation error
You can't have arrays of generic classes. Java simply doesn't support it.
You should consider using a collection instead of an array. For instance,
public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();
Another "workaround" is to create an auxilliary class like this
class MyObjectArrayList extends ArrayList<MyObject> { }
and then create an array of MyObjectArrayList
.
Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:
List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);
Error generic array creation
You can't create arrays with generics. Use a Collection<Set<Hexagon>>
or (Array)List<Set<Hexagon>>
instead.
Here's the formal explanation.
Compilation error: Generic array creation
if you want an array of arraylist:
import java.util.ArrayList;
import java.util.List;
public class Foo{
List [] arrayOfLists = new ArrayList[10];
}
Here is a related post. you cant create a generic array of arraylist.
You can do this:
import java.util.ArrayList;
import java.util.List;
public class Foo{
ArrayList<ArrayList<String>> ll = new ArrayList<ArrayList<String>>();
}
Effective Java: What is exactly generics array creation warning
The warning you're referring to is due to the automated array creation that happens in a varargs method, one like this:
public void method(List<String>... listArgs) { ... }
If you call:
method(new ArrayList<String>(), new ArrayList<String>());
You will see the warning (the actual warning text varies according to Java versions, I think):
[unchecked] unchecked generic array creation for varargs parameter of type List<String>[]
What Java does with varargs methods is to create an array of the variable argument parameter, so in this case it will try to create a List<String>[]
, which is a generic array and thus the warning message (because it will be declassed to List<?>[]
).
How to create a generic array in Java?
I have to ask a question in return: is your GenSet
"checked" or "unchecked"?
What does that mean?
Checked: strong typing.
GenSet
knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with aClass<E>
argument, and methods will throw an exception when they are passed arguments that are not of typeE
. SeeCollections.checkedCollection
.-> in that case, you should write:
public class GenSet<E> {
private E[] a;
public GenSet(Class<E> c, int s) {
// Use Array native method to create array
// of a type only known at run time
@SuppressWarnings("unchecked")
final E[] a = (E[]) Array.newInstance(c, s);
this.a = a;
}
E get(int i) {
return a[i];
}
}Unchecked: weak typing. No type checking is actually done on any of the objects passed as argument.
-> in that case, you should write
public class GenSet<E> {
private Object[] a;
public GenSet(int s) {
a = new Object[s];
}
E get(int i) {
@SuppressWarnings("unchecked")
final E e = (E) a[i];
return e;
}
}Note that the component type of the array should be the erasure of the type parameter:
public class GenSet<E extends Foo> { // E has an upper bound of Foo
private Foo[] a; // E erases to Foo, so use Foo[]
public GenSet(int s) {
a = new Foo[s];
}
...
}
All of this results from a known, and deliberate, weakness of generics in Java: it was implemented using erasure, so "generic" classes don't know what type argument they were created with at run time, and therefore can not provide type-safety unless some explicit mechanism (type-checking) is implemented.
Related Topics
Differencebetween String and Stringbuffer in Java
Spring - How to Use Multiple Transaction Managers in the Same Application
Stream Filter of 1 List Based on Another List
Count Int Occurrences with Java8
Why String.Replaceall() in Java Requires 4 Slashes "\\\\" in Regex to Actually Replace "\"
Why Does Java Code with an Inner Class Generates a Third Someclass$1.Class File
Difference Between Webdriver.Get() and Webdriver.Navigate()
Eventlisteners Using Hibernate 4.0 with Spring 3.1.0.Release
Getting Class Cast Exception Where Both Classes Are Exactly the Same
Java List Sorting: How to Keep a List Permantly Sorted Automatically Like Treemap
Java:Read Last N Lines of a Huge File
Making Spring-Data-Mongodb Multi-Tenant
Extract Integer Part in String
How to Modify the Raw Xml Message of an Outbound Cxf Request
Are Two Java Objects with Same Hashcodes Not Necessarily Equal