Getting the array length of a 2D array in Java

前端 未结 12 2302
耶瑟儿~
耶瑟儿~ 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:52

    If you have this array:

    String [][] example = {{{"Please!", "Thanks"}, {"Hello!", "Hey", "Hi!"}},
                           {{"Why?", "Where?", "When?", "Who?"}, {"Yes!"}}};
    

    You can do this:

    example.length;
    

    = 2

    example[0].length;
    

    = 2

    example[1].length;
    

    = 2

    example[0][1].length;
    

    = 3

    example[1][0].length;
    

    = 4

提交回复
热议问题