Creating an array of vectors

D

Dan

I've tried:

private Vector<Vertex>[] anArray = null;

as a field in a class which works fine. But when I try to initialize
it in the constructor for that class with:

this.anArray = new Vector<Vertex>[];

It doesn't work. The error is "Cannot create a generic array of
Vector<Vertex>."

Vertex is a custom class that I've made. Hopefully it isn't a Sun Java
class. If it is I haven't imported it so I don't know why it would be
getting confused if that was the case.

Any help would be great.

Thanks,

-Dan
 
M

Martijn

Dan said:
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. But something tells me that isn't what you
wanted to do, so instead you may have meant this:

private Vector<Vertex> anArray = null;
anArray = new Vector<Vertex>();

This creates a Vector with no pre-allocated size. If you can and would want
to optimize your program, you could pre-allocated some of the Vector by
issuing the following instead:

array = new Vector<Vertex>(100);

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

Hope this helps,
 
M

Mark Rafn

Dan said:
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 said:
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 said:
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.
 
L

Lew

Martijn said:
(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
 
T

Tom Hawtin

Mark said:
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 said:
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
 
M

Martijn

Lew said:
Martijn said:
(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.
[snipped the explanation why this is not true - M]
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,
 
M

Mark Rafn

Mark said:
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 :)

Tom Hawtin said:
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.
 
T

Tom Hawtin

Mark said:
Tom Hawtin said:
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
 
M

Mark Rafn

Tom Hawtin said:
Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];
Mark said:
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
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!
 
T

Tom Hawtin

Mark said:
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
 
V

vahan

You can write some thing like this:
Vector<Vertex> v1 = new Vector<Vertex>();
private Vector<Vertex>[] anArray = null;

in method :
anArray = new Vector[10];
anArray[0] = v1;

it works.
 
M

Mark Rafn

vahan said:
You can write some thing like this:
Vector<Vertex> v1 = new Vector<Vertex>();
private Vector<Vertex>[] anArray = null;

in method :
anArray = new Vector[10];
anArray[0] = v1;
it works.

But it's not generic, and you have to cast things when you get stuff out
of the array.
 
Joined
Sep 26, 2010
Messages
1
Reaction score
0
I think , You don't need array of vector at all. you can use Vector of Vector.
Like this,
Vector<Vertex> v1 = new Vector<Vertex>();
Vector<Vector<Vertex>> ......

it's more or less like 2D array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top