0

Possible Duplicate:
Java 1.6: Creating an array of List<T>

How can I initialize this array in Java.

Vector<Integer>[] c;

I already try:

Vector<Vector<Integer>[]> a = new Vector<Vector<Integer>[]>();
Vector<Integer>[] c = (Vector<Integer>[])a.toArray();

with the following error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.util.Vector; at app.Program.main(Program.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

EDIT:

My problems is that I am client from a class that receives an generic array "T[] args" argument, and in my case T is a generic type such Vector, Thanks

I am new in Java.

Community
  • 1
  • 1
  • What are you trying to accomplish? A Vector and Array are similar conceptually, but they are not identical. Are you really trying to create a Vector that contains Integer arrays? – Aaron Kurtzhals Jan 09 '13 at 14:52
  • Why would you use an array if you're using a collection? From what you've asked, it seems as if you want an array of vectors. Wouldn't a List of Vectors make more sense? Hard to say w/o the specifics of why you want an array. – Michael Jan 09 '13 at 14:53
  • @Aaron Kurtzhals No, I am trying to create an array of a generic type. – Eric Javier Hernandez Saura Jan 09 '13 at 15:05

4 Answers4

4

I don't think you can create an array of generics. Check this out. Java 1.6: Creating an array of List<T>

This is because the type information isn't available at runtime.

Community
  • 1
  • 1
jack_carver
  • 1,510
  • 2
  • 13
  • 28
2

I wouldn't use Vector if you can avoid it. It was replaced by ArrayList in Java 1.2 (1998)

List<List<Integer>[]> a = new ArrayList<List<Integer>[]>();
List<Integer>[] c = (List<Integer>[]) a.toArray(new List[a.size()]);

toArray() only returns a Object[] which cannot be down cast to a specific array type.

BTW If efficiency is an issue, I would avoid using Integer in a collection. You could use trove instead. TIntArrayList is a wrapper for int[]

List<TIntArrayList> a = new ArrayList<>();
TIntArrayList[] c = a.toArray(new TIntArrayList[a.size()]);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

This is an array of Vectors:

    Vector<Integer>[] c = new Vector[2];
    c[0] = new Vector<Integer>();
    c[0] = new Vector<Object>(); // <- compiletime error
    c[0].add(new Integer(0));

We cannot create a "generic array". But the line above creates an array that holds up to size vectors. And each array slot can hold a reference to a parametized Vector.

We see, that even if the array itself is not generic, the variable c is declared as a generic type this forces us to only put Vector<Integer> instances to the array.

So we can add other vectors to the array, if we ignore some warnings...

    Vector[] b = c;                    // <- warning
    Vector<Double>[] a = b;            // <- warning
    a[1] = new Vector<Double>();       // <- the array itself allows that
    a[1].add(new Double(0));

    for (Vector<Integer> vector : c) {
        Integer value = vector.get(0); // <- BOOM! (on second iteration)
    }
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

You could either remove the generic declaration

Vector[] c = new Vector[10];

Or replace the array with another vector or list type

List<Vector<Integer>> list = new ArrayList<Vector<Integer>>();
Waleed Almadanat
  • 1,027
  • 10
  • 24