How do you copy multidimension array from Vector to Object[][] ?

Discussion in 'Java' started by Ronin, Jan 4, 2007.

  1. Ronin

    Ronin Guest

    I have absolutely no idea how to do this. I inserted a [] array into a
    vector, which then ofcourse became a [][] array, but now can't get it
    out back to a Object[][].


    Vector vector = new Vector();
    vector.addElement(new Object[]{"A","B"});
    vector.addElement(new Object[]{"C","D"});

    Object obj[] = new Object[m.size()]; //this works so far

    m.copyInto((Object[]) obj); //this works so far
    Object[][] attribute = (Object[][]) obj; //BANG!!!

    why the hell wont it work?

    for now I'll just use the crappy m.elementAt() function and cast to [].
    All this casting in a loop eats CPU though.
    Ronin, Jan 4, 2007
    #1
    1. Advertising

  2. Ronin

    Chris Guest

    Re: How do you copy multidimension array from Vector to Object[][]?

    Ronin wrote:
    > I have absolutely no idea how to do this. I inserted a [] array into a
    > vector, which then ofcourse became a [][] array, but now can't get it
    > out back to a Object[][].
    >
    >
    > Vector vector = new Vector();
    > vector.addElement(new Object[]{"A","B"});
    > vector.addElement(new Object[]{"C","D"});
    >
    > Object obj[] = new Object[m.size()]; //this works so far
    >
    > m.copyInto((Object[]) obj); //this works so far
    > Object[][] attribute = (Object[][]) obj; //BANG!!!
    >
    > why the hell wont it work?
    >
    > for now I'll just use the crappy m.elementAt() function and cast to [].
    > All this casting in a loop eats CPU though.
    >


    Some random, maybe not-useful ideas:

    -- Don't use Vector. Use ArrayList.

    -- ArrayList.toArray() is helpful

    -- One of the problems with the Collection classes is that you have to
    cast everything. Generics don't help, because it still does a cast
    internally. If you have a performance-intensive app, don't use
    Collections. Write your own wrapper around an array of the proper type.
    (But *only* if know for sure that casts are causing a performance problem).
    Chris, Jan 4, 2007
    #2
    1. Advertising

  3. Ronin

    Ronin Guest

    Hi Chris thanks but I cant use ArrayList as I'm using J2ME. The API
    does not have that class.

    the thing is im reading data from a server and I dont know the size
    beforehand. So I'm first dropping it into a Vector than move it to a
    Object[][] so to save the whole casting shebang when im iterating
    through it.

    If there is no solution to port to Object[][] from vector.copyInto,
    then yes I'll have to write my own little Vector class. I'll keep my
    eye on this thread for a while.

    cheers



    Chris wrote:
    > Ronin wrote:
    > > I have absolutely no idea how to do this. I inserted a [] array into a
    > > vector, which then ofcourse became a [][] array, but now can't get it
    > > out back to a Object[][].
    > >
    > >
    > > Vector vector = new Vector();
    > > vector.addElement(new Object[]{"A","B"});
    > > vector.addElement(new Object[]{"C","D"});
    > >
    > > Object obj[] = new Object[m.size()]; //this works so far
    > >
    > > m.copyInto((Object[]) obj); //this works so far
    > > Object[][] attribute = (Object[][]) obj; //BANG!!!
    > >
    > > why the hell wont it work?
    > >
    > > for now I'll just use the crappy m.elementAt() function and cast to [].
    > > All this casting in a loop eats CPU though.
    > >

    >
    > Some random, maybe not-useful ideas:
    >
    > -- Don't use Vector. Use ArrayList.
    >
    > -- ArrayList.toArray() is helpful
    >
    > -- One of the problems with the Collection classes is that you have to
    > cast everything. Generics don't help, because it still does a cast
    > internally. If you have a performance-intensive app, don't use
    > Collections. Write your own wrapper around an array of the proper type.
    > (But *only* if know for sure that casts are causing a performance problem).
    Ronin, Jan 5, 2007
    #3
  4. Ronin

    Ronin Guest

    Christ! what a good night of sleep can do!

    Here the solution:

    Vector vector = new Vector();
    vector.addElement(new Object[]{"A","B"});
    vector.addElement(new Object[]{"C","D"});
    Object obj[][] = new Object[vector.size()][2];
    vector.copyInto((Object[]) obj);



    Ronin wrote:
    > Hi Chris thanks but I cant use ArrayList as I'm using J2ME. The API
    > does not have that class.
    >
    > the thing is im reading data from a server and I dont know the size
    > beforehand. So I'm first dropping it into a Vector than move it to a
    > Object[][] so to save the whole casting shebang when im iterating
    > through it.
    >
    > If there is no solution to port to Object[][] from vector.copyInto,
    > then yes I'll have to write my own little Vector class. I'll keep my
    > eye on this thread for a while.
    >
    > cheers
    >
    >
    >
    > Chris wrote:
    > > Ronin wrote:
    > > > I have absolutely no idea how to do this. I inserted a [] array into a
    > > > vector, which then ofcourse became a [][] array, but now can't get it
    > > > out back to a Object[][].
    > > >
    > > >
    > > > Vector vector = new Vector();
    > > > vector.addElement(new Object[]{"A","B"});
    > > > vector.addElement(new Object[]{"C","D"});
    > > >
    > > > Object obj[] = new Object[m.size()]; //this works so far
    > > >
    > > > m.copyInto((Object[]) obj); //this works so far
    > > > Object[][] attribute = (Object[][]) obj; //BANG!!!
    > > >
    > > > why the hell wont it work?
    > > >
    > > > for now I'll just use the crappy m.elementAt() function and cast to [].
    > > > All this casting in a loop eats CPU though.
    > > >

    > >
    > > Some random, maybe not-useful ideas:
    > >
    > > -- Don't use Vector. Use ArrayList.
    > >
    > > -- ArrayList.toArray() is helpful
    > >
    > > -- One of the problems with the Collection classes is that you have to
    > > cast everything. Generics don't help, because it still does a cast
    > > internally. If you have a performance-intensive app, don't use
    > > Collections. Write your own wrapper around an array of the proper type.
    > > (But *only* if know for sure that casts are causing a performance problem).
    Ronin, Jan 5, 2007
    #4
  5. Ronin

    Ronin Guest

    Just for those that want to use Vector.toArray() methods .... no such
    thing in J2ME

    its hacking all the way.

    Ronin wrote:
    > Christ! what a good night of sleep can do!
    >
    > Here the solution:
    >
    > Vector vector = new Vector();
    > vector.addElement(new Object[]{"A","B"});
    > vector.addElement(new Object[]{"C","D"});
    > Object obj[][] = new Object[vector.size()][2];
    > vector.copyInto((Object[]) obj);
    >
    >
    >
    > Ronin wrote:
    > > Hi Chris thanks but I cant use ArrayList as I'm using J2ME. The API
    > > does not have that class.
    > >
    > > the thing is im reading data from a server and I dont know the size
    > > beforehand. So I'm first dropping it into a Vector than move it to a
    > > Object[][] so to save the whole casting shebang when im iterating
    > > through it.
    > >
    > > If there is no solution to port to Object[][] from vector.copyInto,
    > > then yes I'll have to write my own little Vector class. I'll keep my
    > > eye on this thread for a while.
    > >
    > > cheers
    > >
    > >
    > >
    > > Chris wrote:
    > > > Ronin wrote:
    > > > > I have absolutely no idea how to do this. I inserted a [] array into a
    > > > > vector, which then ofcourse became a [][] array, but now can't get it
    > > > > out back to a Object[][].
    > > > >
    > > > >
    > > > > Vector vector = new Vector();
    > > > > vector.addElement(new Object[]{"A","B"});
    > > > > vector.addElement(new Object[]{"C","D"});
    > > > >
    > > > > Object obj[] = new Object[m.size()]; //this works so far
    > > > >
    > > > > m.copyInto((Object[]) obj); //this works so far
    > > > > Object[][] attribute = (Object[][]) obj; //BANG!!!
    > > > >
    > > > > why the hell wont it work?
    > > > >
    > > > > for now I'll just use the crappy m.elementAt() function and cast to [].
    > > > > All this casting in a loop eats CPU though.
    > > > >
    > > >
    > > > Some random, maybe not-useful ideas:
    > > >
    > > > -- Don't use Vector. Use ArrayList.
    > > >
    > > > -- ArrayList.toArray() is helpful
    > > >
    > > > -- One of the problems with the Collection classes is that you have to
    > > > cast everything. Generics don't help, because it still does a cast
    > > > internally. If you have a performance-intensive app, don't use
    > > > Collections. Write your own wrapper around an array of the proper type.
    > > > (But *only* if know for sure that casts are causing a performance problem).
    Ronin, Jan 5, 2007
    #5
    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. Faizan Ahmed

    Bind multidimension Array to Datagrid

    Faizan Ahmed, Oct 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    527
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=
    Oct 25, 2004
  2. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  3. foker
    Replies:
    29
    Views:
    783
    foker
    Sep 9, 2006
  4. Andy Gibbs
    Replies:
    4
    Views:
    1,105
    Larry Evans
    May 20, 2011
  5. Dark Ambient
    Replies:
    10
    Views:
    194
    Dark Ambient
    Aug 13, 2006
Loading...

Share This Page