Creating an array of vectors

Discussion in 'Java' started by Dan, Mar 18, 2007.

  1. Dan

    Dan Guest

    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
     
    Dan, Mar 18, 2007
    #1
    1. Advertising

  2. Dan

    Martijn Guest

    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
     
    Martijn, Mar 18, 2007
    #2
    1. Advertising

  3. Dan

    Mark Rafn Guest

    >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/>
     
    Mark Rafn, Mar 18, 2007
    #3
  4. Dan

    Lew Guest

    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
     
    Lew, Mar 18, 2007
    #4
  5. Dan

    Tom Hawtin Guest

    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
     
    Tom Hawtin, Mar 18, 2007
    #5
  6. Dan

    Martijn Guest

    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
     
    Martijn, Mar 18, 2007
    #6
  7. Dan

    Mark Rafn Guest

    >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/>
     
    Mark Rafn, Mar 18, 2007
    #7
  8. Dan

    Tom Hawtin Guest

    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
     
    Tom Hawtin, Mar 18, 2007
    #8
  9. Dan

    Mark Rafn Guest

    >> 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/>
     
    Mark Rafn, Mar 19, 2007
    #9
  10. Dan

    Tom Hawtin Guest

    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
     
    Tom Hawtin, Mar 19, 2007
    #10
  11. Dan

    vahan Guest

    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.

    Dan wrote:
    > 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
     
    vahan, Mar 19, 2007
    #11
  12. Dan

    Mark Rafn Guest

    vahan <> wrote:
    >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.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Mar 19, 2007
    #12
  13. Dan

    devil_Jit

    Joined:
    Sep 26, 2010
    Messages:
    1
    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.
     
    devil_Jit, Sep 26, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    13,617
    AlyBrat
    Apr 13, 2009
  2. Shannon
    Replies:
    2
    Views:
    517
    Shannon
    Feb 9, 2007
  3. gio

    .Array of Vectors

    gio, Nov 10, 2007, in forum: C Programming
    Replies:
    0
    Views:
    570
  4. Replies:
    3
    Views:
    700
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    444
    Guest
    Sep 14, 2005
Loading...

Share This Page