Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Creating an array of vectors

Thread view: 
Dan - 18 Mar 2007 03:12 GMT
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
Martijn - 18 Mar 2007 11:24 GMT
> 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,

Signature

Martijn
http://www.sereneconcepts.nl

Mark Rafn - 18 Mar 2007 16:05 GMT
>> 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
Lew - 18 Mar 2007 16:26 GMT
> (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

vahan - 19 Mar 2007 07:45 GMT
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.

> I've tried:
>
[quoted text clipped - 17 lines]
>
> -Dan
Mark Rafn - 19 Mar 2007 19:42 GMT
>You can write some thing like this:
>Vector<Vertex> v1 = new Vector<Vertex>();
[quoted text clipped - 4 lines]
>anArray[0] = v1;
>it works.

But it's not generic, and you have to cast things when you get stuff out
of the array.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.