Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to get the length of a 2D array in Java

3 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        try {
 
            int[][] arr2d = new int[3][7];
 
            int rows = arr2d.length;
            int columns = arr2d[0].length;
 
            System.out.println("rows = " + rows);
            System.out.println("columns = " + columns);
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}




/*
run:

rows = 3
columns = 7

*/

 



answered Nov 25, 2016 by avibootz
edited Nov 10, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
       try {
 
            int[][] arr = new int[][]{new int[]{0, 1, 2, 3, 4},
                                      new int[]{5, 6, 7, 8},
                                      new int[]{9, 10},};
 
            System.out.println("rows = " + arr.length);
            System.out.println("row 0 = " + arr[0].length); 
            System.out.println("row 1 = " + arr[1].length); 
            System.out.println("row 2 = " + arr[2].length); 
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}




/*
run:

rows = 3
row 0 = 5
row 1 = 4
row 2 = 2

*/

 



answered Nov 25, 2016 by avibootz
edited Nov 10, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int[][] array = {{1, 2, 3, 4}, {7, 8, 0, 2}, {6, 5, 18, 30}};
        
        int rows = array.length; 
        
        int columns = array[0].length; 

        System.out.println("rows = " + rows);
        System.out.println("columns = " + columns);
    }
}




/*
run:

rows = 3
columns = 4

*/

 



answered Nov 10, 2023 by avibootz

Related questions

1 answer 101 views
1 answer 124 views
1 answer 145 views
1 answer 136 views
136 views asked Jan 3, 2022 by avibootz
1 answer 130 views
130 views asked Nov 19, 2020 by avibootz
1 answer 160 views
160 views asked Oct 24, 2016 by avibootz
...