
Signature
Martijn
http://www.sereneconcepts.nl
>> 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>."
>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/>
Tom Hawtin - 18 Mar 2007 16:48 GMT
> 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 :)
Arrays work differently to generics:
Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
Object[] stuff = vertexVectors;
stuff[0] = new Vector<Cheese>();
Vector<Vertex> erm = vertexVectors[0]; // ??
Should erm be of type Vector<Vertex> or Vector<Cheese>?
The generics equivalent:
Vector<Vector<Vertex>> vertexVectors = new Vector<Vector<Vertex>>();
vertexVectors.add(new Vector<Vertex>);
Vector<?> stuff = vertexVectors;
stuff.set(0, new Vector<Cheese>()); // Will not compile.
Vector<Vertex> erm = vertexVectors.get(0);
> 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
Yup. Indeed, in general, you want to avoid arrays of reference types.
think of arrays (of references) as low level implementation details that
you don't want to bother your code with (unless it's low level code
itself, such as an implementation of an ArrayList).
Tom Hawtin
Mark Rafn - 18 Mar 2007 21:29 GMT
>> 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 :)
> Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
Did you compile this? I get the above error: generic array creation. That's
the puzzle the OP asked about, before we all started helpfully pontificating
on generalities.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Tom Hawtin - 18 Mar 2007 22:04 GMT
>> Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
>
> Did you compile this? I get the above error: generic array creation. That's
> the puzzle the OP asked about, before we all started helpfully pontificating
> on generalities.
No. The point is that the statement doesn't make sense. It's a reductio
ad absurdum type thingy.
Tom Hawtin
Mark Rafn - 19 Mar 2007 03:00 GMT
>>> Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
>> Did you compile this? I get the above error: generic array creation. That's
>> the puzzle the OP asked about, before we all started helpfully pontificating
>> on generalities.
>No. The point is that the statement doesn't make sense. It's a reductio
>ad absurdum type thingy.
I didn't see the absurdum part. In the (rare) case that you WANT an array of
Vector of Vertex, how should you do so? The ability to do:
Vertex v = vertexVectors[0].get(0)
Without explicit casting might be handy for someone!
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Tom Hawtin - 19 Mar 2007 05:19 GMT
> I didn't see the absurdum part. In the (rare) case that you WANT an array of
> Vector of Vertex, [...]
An array of Vector of Vertex is absurd.
Tom Hawtin
> (a
> Vector is not a good solution for frequently growing arrays, although it
> provides better random access than ArrayList).
Huh? This is not true at all. Vector provides the same random access as
ArrayList, and the same solution for "frequently growing {sic}" arrays.
Vector carries some cruft from its pre-Collection days, but that has nothing
whatsoever to do with an "array" nor with how it grows nor with random access.
The difference is that Vector methods are synchronized on the Vector
instance and ArrayList methods are not synchronized.
To the OP - always double-check newsgroup answers. Even (perhaps especially?)
this one. "Trust but verify."
-- Lew
Martijn - 18 Mar 2007 18:39 GMT
>> (a
>> Vector is not a good solution for frequently growing arrays,
[quoted text clipped - 4 lines]
> To the OP - always double-check newsgroup answers. Even (perhaps
> especially?) this one. "Trust but verify."
Thanks for catching that one (also to Mark). I stand corrected. Maybe I
should get a bit more of a Java mind-set before answering questions
questions. Either way I was a bit hesitant to answer, especially as giving
the wrong answer is worse to not giving any answer at all.
No harm intended, though.
Greets,

Signature
Martijn
http://www.sereneconcepts.nl