Home > Blockchain >  Please explain a[0].length in 2nd loops
Please explain a[0].length in 2nd loops

Time:12-29

public class Tester {
    public static void main(String[] args) {

        int a[][] = { { 1, 3, 4 }, { 2, 3, 6 }, { 7, 6, 5 } };
        int sum = 0;
        for (int i = 0; i < a.length; i  ) {
            for (int j = 0; j < a[0].length; j  ) {
                if (a[i][j] % 2 == 0)
                    break;
                sum  = a[i][j];
            }
        }
        System.out.println("sum = "   sum);
    }
}

CodePudding user response:

a[0] gives you this array: { 1, 3, 4 }, so a[0].length will give you its length, which is 3.

CodePudding user response:

yes I got it answer of my question

a[0].length simply means Zeroth indexs element length

a = { 1, 3, 4 }, { 2, 3, 6 }, { 7, 6, 5 }

So at zeroth index there are 3 elements 1,,3,4 so length is 3

CodePudding user response:

so let's say you have 2d array like this:

a[0] = {1,2,3},
a[1] = {4,5,6},
a[2] = {7,8,9}

a[0].length refers to the length of the zero position array elements which is {1,2,3} thus 3

CodePudding user response:

a.length is the number of rows. a[0].length is the number of columns.

  • Related