Getting the array length of a 2D array in Java

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

    With Java 8, allow you doing something more elegant like this:

    int[][] foo = new int[][] {
            new int[] { 1, 2, 3 },
            new int[] { 1, 2, 3, 4},
        };
    
    int length = Arrays.stream(array).max(Comparator.comparingInt(ArrayUtils::getLength)).get().length
    

提交回复
热议问题