How to print 2d array in Java

0

 Two dimensional arrays are also called as matrix . We can store two dimensional arrays in a matrix . 


Input : {{3,1,2},

             {6,5,2},

             {2,0,8}}


Output : 3,1,2,6,5,2,2,0,8


So we are going to write a program in java which will print each cell of this matrix . So here we have used a 2d array . 



public class Matrix
{
    public static void print(int [][] mat)
    {
        for(int i=0;i<mat.length;i++)
        {
            for(int j=0;j<mat[i].length;j++)
            {
                System.out.print(mat[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void main(String [] args)
    {
        int [][] mat = { {1,2,3,4},{3,4,5},{7,5,2,0}};
        print(mat);
        
    }
}


Output : 1,2,3,4,3,4,5,7,5,2,0

Time Complexity : O(n^2)

Space Complexity : O(1)



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !