0

Hello everyone how can I do to initialize

ArrayList<Record<T>> [] values;

with a number of rows and columns? for example, with three rows and four columns?

2 Answers 2

2

Using an array of generic type (such as ArrayList<T>) is a way to hell. The following line doesn't even compile:

ArrayList<Record<T>>[] values = new ArrayList<Record<T>>[SIZE_ARRAY];

The compiler issues this error:

Cannot create a generic array of ArrayList<Record<T>>

The only sensible solution is to create a list of lists:

// create matrix
ArrayList<ArrayList<Record<T>>> matrix = new ArrayList<ArrayList<Record<T>>>();

// add 3 rows:
for (int i = 0; i < 3; i++) {
    ArrayList<Record<T>> row = new ArrayList<Record<T>>();
    matrix.add(row);

    // add 4 elements to each row
    for (int j = 0; j < 4; j++) {
        Record<T> record = ... // get the record
        row.add(record);
    }
}

And now you can access the element at [2][3] (last element of the last row):

Record<T> record = matrix.get(2).get(3);
2
  • "The only sensible solution is to create a list of lists:" No it isn't. It's perfectly possible to create an array of ArrayList.
    – newacct
    Nov 28, 2012 at 19:48
  • @newacct I didn't say it not possible. I said that is not sensible, meaning that generic arrays are not type-safe (and syntactically ugly).
    – Natix
    Nov 28, 2012 at 19:53
0

Just do:

ArrayList<Record<T>>[] values = (ArrayList<Record<T>>[])new ArrayList<?>[SIZE_ARRAY];

There will be an unchecked cast warning, which you can basically ignore.

The real reason why array creation of a parameterized type is disallowed is kind of esoteric. Arrays in Java (unlike generic collections) have (and have always had) a feature that they are checked at runtime -- i.e. every time you put an element into them, it checks that the element is an instance of the array's component type (which it knows at runtime). If not, it throws an exception (ArrayStoreException).

Now, think about arrays of parameterized types. Given you know the above behavior of arrays, you would expect that an array of parameterized type would check that things put into it are instances of that parameterized type. Except that it is impossible in Java to check that something is an instance of a parameterized type (ArrayList<Something>) at runtime. The most you can do is check that it is an ArrayList, but not its parameter.

Since it would not do entirely what people expect, the Java designers decided to ban creating arrays of parameterized types altogether. You can still create an array of a raw type (e.g. new ArrayList[5]) or an array of a wildcarded type (e.g. new ArrayList<?>[5]) because in those cases, it is possible to check if an object is an instance at runtime (i.e. obj instanceof ArrayList and obj instanceof ArrayList<?> are legal, but obj instanceof ArrayList<Something> is not). (They could have also created a special case that arrays of parameterized types only check elements against the raw type, not the parameterized type, but that would create inconsistency.) However, probably most people who run into this problem are not thinking about the ability of arrays to check their element types at runtime -- they probably just want to create an array for normal use.

You can also cast the array back into an array-of-parameterized-type type. It's an unchecked cast, because as explained above, there is no way that an array of parameterized type can fulfill the contract of an array to fully check its element types -- e.g. now if you have an ArrayList<String>[], it is possible for a ArrayList<Integer> to be added to the array without generating an exception. However, 99% of people don't care about this feature, and the unchecked cast says, hey, we know about this limitation, but we want to do it anyway.

2
  • If you say that you can ignore the warnings, then why bother with generics and type safety at all and not go all the way with rawtypes? :)
    – Natix
    Nov 28, 2012 at 19:55
  • @Natix: because the only reason you need to do this unchecked cast concerns an aspect of arrays (runtime checking of element types) which most people don't care about. And a lot of times in Generics, there are things that cannot be expressed in the type system (e.g. a Map that maps classes to elements of the corresponding class type) but that you can implement in a way that you can prove is correct.
    – newacct
    Nov 28, 2012 at 20:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.