printing out a 2-D array in Matrix format

后端 未结 9 1440
暖寄归人
暖寄归人 2020-12-08 03:13

How can I print out a simple int [][] in the matrix box format like the format in which we handwrite matrices in. A simple run of loops doesn\'t apparently work. If it helps

9条回答
  •  悲&欢浪女
    2020-12-08 03:50

    public static void main(String[] args) 
            {
                 int [] [] ar= 
                    {
                            {12,33,23},
                            {34,56,75},
                            {14,76,89},
                            {45,87,20}
    
                    };
    

    I prefer using enhanced loop in Java

    Since our ar is an array of array [2D]. So, when you iterate over it, you will first get an array, and then you can iterate over that array to get individual elements.

                 for(int[] num: ar)
                 {
                     for(int ele : num)
                     {
                     System.out.print(" " +ele);
                     }
                     System.out.println(" " );
                 }
    
                  }
    

提交回复
热议问题