multidimensional array problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    multidimensional array problem

    i want to store numbers in a grid. thats not the problem...i need to be able to store a variable amount of numbers at each location...orig inally i set up an int[][][] but the inflexibility of the array made some problems for me...what i need is something like this: int[][]() the ()being an arraylist or something of that nature. Something about this confuses me...is there a way to do that?

    thanks
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by drsmooth
    i want to store numbers in a grid. thats not the problem...i need to be able to store a variable amount of numbers at each location...orig inally i set up an int[][][] but the inflexibility of the array made some problems for me...what i need is something like this: int[][]() the ()being an arraylist or something of that nature. Something about this confuses me...is there a way to do that?

    thanks
    You can have an array of any type, including an array of lists. You can also define arrays piecemeal, like this:

    [CODE=Java]int size = 5;
    int [][] triangle = new int[size][];
    for(int i=0; i<size; ++i) {
    triangle[i] = new int[i+1];
    }[/CODE]

    It's not clear from your post what your real problem is. If it's that you want an array to grow or shrink, that's usually a sign that you should go with a collection like a list instead.

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      yea ill try the arraylist way...thats wat i thought to do i just wasnt sure how it would work...wud i use grid[x][y].get(z)? i guess that makes sense

      thanks alot

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by drsmooth
        yea ill try the arraylist way...thats wat i thought to do i just wasnt sure how it would work...wud i use grid[x][y].get(z)? i guess that makes sense

        thanks alot
        I'm not a big fan of mixing arrays and collections. Actually, I think if you analyze your problem further you'll want to start defining some classes. I've never had to nest arrays three deep: [][][]. Unless this is some specific math problem, I suppose, but I can only guess your domain.

        Comment

        Working...