如何在 Java 中获取二维数组的行数和列数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34698144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:54:49  来源:igfitidea点击:

How to get rows and columns count of a 2D array in Java?

javaarraysmultidimensional-arraysyntax

提问by Aishwarya R

For an array in Java, we can get the lengthof the array with array_name.length. Similarly, how would one get the number of rows and columns of a 2Darray?

对于 Java 中的数组,我们可以length使用array_name.length. 同样,如何获得二维数组的行数和列数?

采纳答案by Idos

Well you probably want array_name.lengthfor getting the count of the rows and array_name[0].lengthfor the columns. That is, if you defined your array like so:

好吧,您可能想要array_name.length获取行数和array_name[0].length列数。也就是说,如果您像这样定义数组:

T[][] array_name = new T[row][col];
array_name.length // row
array_name[0].length // col