Java: creating a multidimensional shared array

How to create a multidimensional array of common elements in java?

Consider the class:

class A<T> { T t; public A(T t) { this.t = t; } } 

When I try to create a multidimensional array:

 A<String>[][] array = new A<String>[2][3]; 

I get the following error:

 generic array creation A<String>[][] array = new A<String>[2][3]; ^ 

I tried the following:

 A<String>[][] array = (A<String>[][]) (new Object[2]3]); 

But it just throws: java.lang.ClassCastException

What is the fix?

(I predict that people recommend using lists. Please explain how to achieve this using arrays.)

+7
source share
7 answers

I was able to do something like this

  @SuppressWarnings("unchecked") A<String>[][] array = (A<String>[][]) Array.newInstance(new A<String>("dummy").getClass(), 2, 3); 

EDIT:

from the @dsg clause, the following will skip creating a temporary object.

  @SuppressWarnings("unchecked") A<String>[][] array = (A<String>[][]) Array.newInstance(A.class, 2, 3); 

or (from @irreputable suggestion)

  @SuppressWarnings("unchecked") A<String>[][] array = new A[2][3]; 
+4
source

You cannot create an array of a typical type in simple form.

 List<String>[] list = new List<String>[2]; //Illegal List<?> aa[] = new List<?>[2]; // OK ... A<?>[][] array = new A<?>[2][3]; // OK A[0][0] = new A<String>(...); 

This is an interesting article about Java 1.5 generators, " Theory and Practice of Java: Generics gotchas "

+2
source

Thanks to the comments, I was able to put together a solution.

As we saw, A<String>[][] array = new A<String>[2][3]; does not work.

Here's how to build an array of 2x3 A<String> objects that works:

 // get the class of the basic object Class c = new A<String>("t").getClass(); // get the class of the inner array A<String>[] a0 = (A<String>[]) java.lang.reflect.Array.newInstance(c, 0); // construct the outer array A<String>[][] array = (A<String>[][]) java.lang.reflect.Array.newInstance(a0.getClass(), 2); // fill it with instances of the inner array for (int i = 0; i < 2; ++ i) { array[i] = (A<String>[]) java.lang.reflect.Array.newInstance(c, 3); } 

Cleaner version (thanks, @Balla R):

 @SuppressWarnings("unchecked") A<String>[][] array = (A<String>[][]) java.lang.reflect.Array.newInstance(A.class,2,3); 
+1
source

Why don't you do something like this: (non-generic)

 String[][] non_generic_array = new String[][]; 

And create a utility class to implement the functions that you did in A<T> (I suppose there is). For example:

When you had it in A :

 public class A<T> { T obj; public A(T obj) { this.obj = obj; } public void someFunction() { ... } } 

You can create a utility class:

 public class AUtils { public static <T> void someFunction(T obj) { // Here your code, applied to obj } } 
+1
source

new A[][] and draw it on A<String>[][]

+1
source

Hmm, I thought Java Arrays (since Java 6) does not support generics. One of my biggest "wtf" when I started programming with generics in java.

0
source
 class A<T> { T s; public A(T t) { s = t; } public String getType() { return s.getClass().toString(); } public T getThing() { return s; } } public static void main(String[] args) { A<?>[][] a = new A<?>[2][3]; a[0][1] = new A<String>("hi"); System.out.println(a[0][1].getType()); System.out.println(a[0][1].getThing()); A<String> b = (A<String>) a[0][1]; } 

exit:

class java.lang.String

Hi

0
source

All Articles