Getting the array length of a 2D array in Java

前端 未结 12 2329
耶瑟儿~
耶瑟儿~ 2020-11-27 09:27

I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:

public class MyClass {

 public s         


        
12条回答
  •  醉酒成梦
    2020-11-27 10:07

    Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:

    if (row > 0) col = test[0].length;
    

提交回复
热议问题