Home > Software design >  what does map[0].length mean?
what does map[0].length mean?

Time:05-24

map.length means how many stuff are in map array but I dont understand map[0].length.

  for (int i=0; i<map.length;i  ) {
                for(int j=0; j<map[0].length;j  ) {
                    
                }
            }

CodePudding user response:

Arrays are zero-indexed, which means the first item is index 0, second is 1, etc.

Therefore map[0].length just gets the length of the first item in the array.

CodePudding user response:

Here, it seems like map is a two dimensional array (an array containing arrays). You could visualize it like this.

      map[0] map[1] map[2] map[3]
        _      _      _      _
map [   0      1      2      3    ]
        1      2      3      4
        2      3      4      5
        5      _      _      _
        _

In this case, map.length would return 4 (because map contains 4 arrays). When you do map[0], you get the array stored at index 0, so in the exemple, map[0] would return [ 0 1 2 5 ]. Knowing that, map[0].length return the length of the array stored at index 0 (in the exemple, 4), but map[1].length would return 3 (because map[1] is [ 1 2 3 ])

  • Related