Multidimensional Arrays lengths in Java

后端 未结 8 1091
一个人的身影
一个人的身影 2020-12-04 18:15

How to find the lengths of a multidimensional array with non equal indices?

For example, I have int[][] pathList = new int[6][4]

Without actuall

8条回答
  •  情歌与酒
    2020-12-04 18:56

    Java has "jagged" multidimensional arrays, which means that each "row" in your two-dimensional array can have a different number of components. If you can assume that each row has the same number of components, use:

    pathList[0].length;
    

    Otherwise, you will have to iterate:

    int maxRowLength = 0;
    for (int i = 0; i < pathList.length; i++) {
        if (maxRowLength < pathList[i].length) {
            maxRowLength = pathList[i].length;
        }
    }
    

提交回复
热议问题