9

So, I want an array of Vector of Integer in Java.

If I put

Vector<Integer>[] matrix;
matrix = new Vector<Integer>[100];

I get cannot the compilation error

cannot create a generic array of Vector

Should I use

    matrix = new Vector[100];

instead? (which gives a warning)

Or should I simply not use an array of vectors and use vector of vector instead?

Note: I don't want a Vector< Integer >, I want a Vector< Integer >[] to create a matrix of Integers without using Integer[][].

4
  • 1
    am I the only one who struggled to follow this train of thought? Commented Jun 1, 2011 at 14:59
  • You should really use List instead of Vector. Is there a particular reason you aren't using an array of arrays, or a v̶e̶c̶t̶o̶r̶ ̶o̶f̶ ̶v̶e̶c̶t̶o̶r̶s̶ list of lists? Commented Jun 1, 2011 at 15:00
  • List is an interface, you probably mean ArrayList. Commented Jun 1, 2011 at 15:01
  • possible duplicate of Java Generics Syntax for arrays Commented Jun 1, 2011 at 19:14

5 Answers 5

6

Java simply doesn't have any means to create arrays of a parameterized type without getting or suppressing a warning. So the best you can get is this:

@SuppressWarnings("unchecked")
Vector<Integer>[] anArray = (Vector<Integer>[]) new Vector<Integer>[100];

You can get around this problem if you avoid arrays entirely. I.e.:

Vector<Vector<Integer>> list = new Vector<Vector<Integer>>(100);

Or with the collection types:

List<List<Integer>> list = new ArrayList<List<Integer>>(100);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I think suppressing the warning or simply using vector of vector is the only way.
I know that the answer is 10 years old but the first solution no longer compiles.
3
Vector<Integer> vector = new Vector<Integer>();

If you try to do something like this:

Vector<Integer> vector = new Vector<Integer>();
Vector<Integer>[] vectors = {vector};

You will get a compile error:

Cannot create a generic array of Vector

However if you don't specify the generic type java will allow it but with a warning:

Vector<Integer> vector = new Vector<Integer>();
Vector[] vectors = {vector};

5 Comments

That only creates a single vector, not an array of vectors.
this seems to be want he wants. I'm still confused as to what he really means by "an array of vector".
I need an array of vectors, not just a vector. It's similar to Integer[][], but with the inner array being a vector instead of an array, because it needs to increase. I could just use Vector of Vector, but the outer one won't increase so it could just be an array.
Yes, the last works, but Vector< Vector< Integer > > is better I believe.
Generic arrays is not allowed in java is wrong. Arrays of parameterized types are allowed and essential to the Java language. After all the array based collection classes use them internally (mostly implicit but sometimes explicit).
1

Vectors are backed by arrays, and will grow or shrink to a size sufficent to hold the element you insert into it. As such, you can pre-allocate a Vector, but you do not have to actually specify the size at create time.

// preallocated vector, which can hold 100 elements
Vector<Integer> integers = new Vector(100);

.

// default vector, which will probably grow a couple of times when adding 100 element
Vector<Integer> integers = new Vector();

A true Java array cannot grow or shrink, and it doesn't support removal of an element from a mid-point. To allocate an Array, you use

// allocate an array
Integer[] integers = new Integer[100];

Now if you want to have an "array of vectors" then you would

// array of vectors
Vector[] vectors = new Vector[100];

Comments

0

To create an array of generic you have to create the non-generic and cast it. You also have to initialise all the elements in the array, otherwise they will be null. :(

Vector<Integer>[] anArray = (Vector<Integer>[]) new Vector[100];
for(int i = 0; i < anArray.length; i++)
   anArray[i] = new Vector<Integer>();

However, since Vector is a legacy class which was replaced by ArrayList in Java 1.2 (1998) I would use List for the interface and ArrayList for the implementation.

List<Integer>[] anArray = (List<Integer>[]) new List[100];
for(int i = 0; i < anArray.length; i++)
   anArray[i] = new ArrayList<Integer>();

Another option would be to use a collection which held primitive int instead of Integer Objects. This can enhance performance if you need it.

TIntArrayList[] anArray = new TIntArrayList[100];
for(int i = 0; i < anArray.length; i++)
   anArray[i] = new TIntArrayList();

Comments

0

To avoid type casting, consider this implementation:

    Vector<Integer>[] intVectorArray;
    Vector[] temp = new Vector[desiredSize];
    intVectorArray = temp;
    for(int i = 0;i<intVectorArray.length;i++){
        hashArray[i] = new Vector<Integer>();
    }

The newly created intVectorArray will inherit the generic Vector-Array type of temp to give you your desired dimensions, and the for loop will instantiate your desired datatype.

When you're ready to call Integer functions on elements of intVectorArray, you will be all set!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.