Results 1 to 2 of 2
  1. #1
    seeder is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Question 2d dynamic arrays

    I'm not sure where to put this so i'll put it here, I've been using java for about 4-5 years now and never had to deal with a problem like this:confused:

    problem....i need a 2d array where all rows will have different number of collums, meaning

    newArr[0][3] where 3 is max element
    newArr[1][7] where 7 is max element
    newArr[2][1] where 1 is max element

    i've tried looking thought help files but they only tell you about how to transfer to an ArrayList and not how to bring it back, nor what to do in case of 2/3D arrays

    i'll be happy to post any additional info you need
    thanks in advance :D

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, the first array doesn't care how long the arrays that it contains are, so simply assign the "inner" array as a whole on creation.

    Java Code:
    Whatever[][] arrays = new Whatever[10][];
    arrays[0] = new Whatever[4];
    arrays[1] = new Whatever[7];
    arrays[2] = new Whatever[2];
    arrays[3] = new Whatever[25];
    ...
    Now, if the length of the "inner" and "outer" arrays can change, then use ArrayLists

    Java Code:
    List<List<Whatever>> outer = new ArrayList<List<Whatever>>();
    outer.add(new ArrayList<Whatever>());
    outer.add(new ArrayList<Whatever>());
    outer.add(new ArrayList<Whatever>());
    outer.add(new ArrayList<Whatever>());
    outer.get(1).add(new Whatever());
    outer.get(1).add(new Whatever());
    outer.get(1).add(new Whatever());
    outer.get(1).add(new Whatever());
    Whatever whatever = outer.get(1).get(2);

Similar Threads

  1. dynamic two dimensional arrays?
    By dinosoep in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 06:12 PM
  2. Dynamic Classes
    By nunofaria in forum Advanced Java
    Replies: 8
    Last Post: 11-17-2009, 06:20 AM
  3. Dynamic GUI
    By ike2u in forum New To Java
    Replies: 4
    Last Post: 08-08-2009, 02:50 AM
  4. Dynamic JTable
    By eftenpuften in forum AWT / Swing
    Replies: 0
    Last Post: 07-24-2008, 03:41 PM
  5. Help for dynamic classes
    By Bender in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2007, 08:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •