CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    10

    Smile Multi Dimension Arraylist

    Hi,

    I am trying to come up with a better way to store a score into a table that have X and Y axis.
    It's just like 2D array, a score would be stored at the X's row and Y's column and I should be able to get that score using row and column number:

    ie.
    0 1 2 3 4
    1 X X X X
    2 X X X X
    3 X X X X
    4 X X X X

    but here is my problem, 2D array work fine when I know the array size, but my input's size
    depend on the input file that needs to be loaded into the application and their size are not fixed.

    So right now, I end up using multiple ArrayList inside an ArrayList to simulate how the 2D array work, which solve the unfix input's size problem . But it just seem to be quite a messy way to solve this problem for me, so I was just wondering are there any other ways I could go about this?

    Cheer!

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Multi Dimension Arraylist

    In what way do you find it messy? An ArrayList is just a class wrapper around an internal array; it provides auto array-resizing and other facilities to make life simpler. It has to use the method syntax to access the array elements because it's a class.

    The outcome of any serious research can only be to make two questions grow where only one grew before...
    T. Veblen
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2010
    Posts
    10

    Re: Multi Dimension Arraylist

    Ah well, may be it's just the way I have to declare and
    access the values in the list. I thought it look kinda long
    and may be there is a better way to go about this O.o


    But never mind, I think I'll just stick to multiple ArrayList.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Multi Dimension Arraylist

    Ah well, may be it's just the way I have to declare and
    access the values in the list.
    If it is a real problem for you, you could always convert the ArrayList of ArrayLists to a 2D array once you have read all the values in.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Multi Dimension Arraylist

    Quote Originally Posted by holypromise View Post
    So right now, I end up using multiple ArrayList inside an ArrayList to simulate how the 2D array work, which solve the unfix input's size problem.
    Do you really need a two-dimensional data structure? Can't you work with a one-dimensional data structure and a two-dimensional index function like.
    Code:
    int index(int row, int col) {
       return row*MAX_ROWS + col;
    }
    You have one ArrayList and use the index function to map row,col pairs on a linear index for access to the ArrayList. This is how accesses to multi-dimensional arrays are handled under the hood in many languages.
    Last edited by nuzzle; July 5th, 2010 at 03:57 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured