Getting the array length of a 2D array in Java

前端 未结 12 2339
耶瑟儿~
耶瑟儿~ 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 09:55

    There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.

    You could easy create your own class to abstract the functionality you need.

    If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.

提交回复
热议问题