Java: Declaring a multidimensional array without specifying the size of the array ( eg. new int[10][] )

前端 未结 6 1252
忘掉有多难
忘掉有多难 2021-01-13 09:08

I\'ve been trying to figure out what exactly is happening here. I\'m just trying to figure out what the 2 lines are doing that I\'ve commented on below. I found this program

相关标签:
6条回答
  • 2021-01-13 09:15

    It is better to use multidimensional collection in Java.

    Syntax:

    ArrayList <Object> x= new ArrayList <Object>();

    Below is an application of multidimensional collection in Java.

    
    import java.util.*; 
    
    class MultidimensionalArrayList { 
    
        /*function for creating and returning 2D ArrayList*/
        static List create2DArrayList() 
        { 
            /*Declaring 2D ArrayList*/
            ArrayList<ArrayList<Integer> > x 
                = new ArrayList<ArrayList<Integer> >(); 
    
            /*one space allocated for 0th row*/
            x.add(new ArrayList<Integer>()); 
    
            /*Adding 3 to 0th row created above x(0, 0)*/
            x.get(0).add(0, 3); 
    
            /*Creating 1st row and adding values 
        (another way for adding values in 2D collections)*/
            x.add(new ArrayList<Integer>(Arrays.asList(3, 4, 6))); 
    
            /*Add 366 to 1st row 0th column x(1, 0)*/
            x.get(1).add(0, 366); 
    
            /*Add 576 to 1st row 4th column x(1, 4)*/
            x.get(1).add(4, 576); 
    
            /*Adding values to 2nd row*/
            x.add(2, new ArrayList<>(Arrays.asList(3, 84))); 
    
            /*Adding values to 3rd row*/
            x.add(new ArrayList<Integer>(Arrays.asList(83, 6684, 776))); 
    
            /*Adding values to 4th row*/
            x.add(new ArrayList<>(Arrays.asList(8))); 
    
            return x; 
        } 
    
        public static void main(String args[]) 
        { 
            System.out.println("2D ArrayList :"); 
    
            /*Printing 2D ArrayList*/
            System.out.println(create2DArrayList()); 
        } 
    } 
    
    
    
    0 讨论(0)
  • 2021-01-13 09:17

    All new int[10][] is declaring is an array of size 10, containing null arrays.

    In the for loop, the null arrays are being instantiated into ever increasing array sizes.

    0 讨论(0)
  • 2021-01-13 09:21

    it's not lacking, it's basically not setting a specific amount, it isn't required because it can have many fields

    and the second line

    tri[r] = new int[r+1];
    

    is setting all the fields to not null

    0 讨论(0)
  • 2021-01-13 09:24

    It's simply declaring an array of 10 arrays. The lengths of each of those "sub" arrays can all be different.

    0 讨论(0)
  • 2021-01-13 09:26

    The first line makes an array of int arrays. There are 10 slots for int arrays created.

    The third line creates a new int array and puts it in one of the slots you made at first. The new int array has r+1 spaces for ints in it.

    So, the int array in position 0 will have 1 slot for an int. The int array in position 1 will have 2 slots for an int. The overall shape will be:

    [
        [0],
        [0,0],
        [0,0,0],
        ...,
        [0,0,0,0,0,0,0,0,0,0]
    ]
    

    which is hinted at with the variable name tri (it looks like a triangle)

    0 讨论(0)
  • 2021-01-13 09:29

    It makes more sense if you think of a multidimensional array as an array of arrays:

    int [][] tri = new int[10][]; // This is an array of 10 arrays
    
    tri[r] = new int[r+1]; // This is setting the r'th array to
                           // a new array of r+1 ints
    
    0 讨论(0)
提交回复
热议问题