Re: Creating an array of vectors
Dan wrote:
private Vector<Vertex>[] anArray = null;
this.anArray = new Vector<Vertex>[];
It doesn't work. The error is "Cannot create a generic array of
Vector<Vertex>."
Martijn <subscription_REMOVE_101@hot_REMOVE_mail.com> wrote:
I don't have my javac handy (so I can't verify this), but I think that
should be:
this.anArray = new Vector<Vertex>[100];
This creates an array of 100 vectors, i.e. it gives you 100 Vectors which
can all be used as arrays.
More accurately, it gives you an array that can hold 100 Vectors of Vertex.
It doesn't give you any actual Vectors, you have to new them all up yourself.
But also, it doesn't work.
import java.util.Vector;
public class Test {
public static class Vertex {}
public static void main(String[] args) {
Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
}
}
Test.java:7: generic array creation
Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
You can specify just "new Vector[1]" and it works with a warning. I don't know
why it's disallowed to create an array of type-specified generic objects.
I hope someone here does :)
I fully agree that the OP may not actually want an array of vectors, but just
a Vector (or ArrayList) of Vertex. Generally, you don't mix array and List
usage, and arrays of lists are just confusing. When you want a
two-dimensional representation, use:
Vertex[][] vertices;
or
List<List<Vertex>> vertices;
On a final note, there are other types of dynamic arrays, such as ArrayList,
but which one suits your program best depends on the way you use it (a
Vector is not a good solution for frequently growing arrays, although it
provides better random access than ArrayList).
Huh? How does Vector provide different access than ArrayList? Vector is
synchronized, but otherwise ArrayList is a drop-in replacement.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>