Getting the array length of a 2D array in Java

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

    public class Array_2D {
    int arr[][];
    public Array_2D() {
        Random r=new Random(10);
         arr = new int[5][10];
         for(int i=0;i<5;i++)
         {
             for(int j=0;j<10;j++)
             {
                 arr[i][j]=(int)r.nextInt(10);
             }
         }
     }
      public void display()
      {
             for(int i=0;i<5;i++)
    
             {
                 for(int j=0;j<10;j++)
                 {
                     System.out.print(arr[i][j]+" "); 
                 }
                 System.out.println("");
             }
       }
         public static void main(String[] args) {
         Array_2D s=new Array_2D();
         s.display();
       }  
      }
    

提交回复
热议问题