Tony DiNitto
Tony DiNitto

Reputation: 1239

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

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 that doesn't declare the full dimensions of the array (instead of new int[10][5]; it just decides to not declare it by saying 'new int[10][];' It's like the 2nd array length doesn't matter (changing it to 1 or 100 doesn't affect the output).

int[][] tri = new int[10][];  //this lack of giving the size of the 2nd array is strange
 for (int r=0; r<tri.length; r++) {
 tri[r] = new int[r+1];   //I'm not sure what this line is doing really 
}
for (int r=0; r<tri.length; r++) {
 for (int a=0; a<tri[r].length; a++) {
     System.out.print(tri[r][a]);  
     }
 System.out.println();
 }

Upvotes: 8

Views: 17475

Answers (6)

Sudam Yasodya
Sudam Yasodya

Reputation: 21

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()); 
    } 
} 


Upvotes: 0

trutheality
trutheality

Reputation: 23465

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

Upvotes: 3

Sheriff
Sheriff

Reputation: 495

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.

Upvotes: 5

Travs
Travs

Reputation: 83

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

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272657

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

Upvotes: 3

Riley Lark
Riley Lark

Reputation: 20890

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)

Upvotes: 12

Related Questions