Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating an array of vectors

Reply
Thread Tools

Creating an array of vectors

 
 
Dan
Guest
Posts: n/a
 
      03-18-2007
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

 
Reply With Quote
 
 
 
 
Martijn
Guest
Posts: n/a
 
      03-18-2007
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>."


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,

--
Martijn
http://www.sereneconcepts.nl


 
Reply With Quote
 
 
 
 
Mark Rafn
Guest
Posts: n/a
 
      03-18-2007
>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 <http://www.dagon.net/>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-18-2007
Martijn wrote:
> (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

 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-18-2007
Mark Rafn wrote:
> 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
 
Reply With Quote
 
Martijn
Guest
Posts: n/a
 
      03-18-2007
Lew wrote:
> Martijn wrote:
>> (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,

--
Martijn
http://www.sereneconcepts.nl


 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      03-18-2007
>Mark Rafn wrote:
>> 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 <> wrote:
> 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 <http://www.dagon.net/>
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-18-2007
Mark Rafn wrote:
>
> Tom Hawtin <> wrote:
>> 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
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      03-19-2007
>> Tom Hawtin <> wrote:
>>> Vector<Vertex>[] vertexVectors = new Vector<Vertex>[1];


>Mark Rafn wrote:
>> 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.


Tom Hawtin <> wrote:
>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 <http://www.dagon.net/>
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-19-2007
Mark Rafn wrote:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Array of vectors jack.smith.sam@gmail.com Java 8 04-13-2009 03:06 PM
c++ primer statement about vectors containing vectors pauldepstein@att.net C++ 3 03-26-2008 06:22 PM
.Array of Vectors gio C++ 0 11-10-2007 11:20 AM
Specify bit position in array of vectors Shannon VHDL 2 02-09-2007 09:38 PM
Ruby, SWIG and C++: how to properly wrap vector of vectors of doubles (2D vectors)? Ruby 0 09-14-2005 05:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57