How to create an array of vector in Java?

2019-04-05 18:12发布

问题:

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[][].

回答1:

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);


回答2:

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};


回答3:

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];


回答4:

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();


回答5:

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!