Write a Program Print 2D Array in Java - Technology369kk

 Print 2D Array 


Program:  

// Program |Code                                                      

import java.util.*;                                                   //import :- java labaries file from java server.  
class TwoDArray{                                                      // class : This is a keyword declare in java it is complasarry.
    public static void main(String args[]){                           // public : is a access modifier and
                                                                     // "static" : is also a keyword but static method executed by JVM and save memory.
                                                                     // " void"  : is return type of method, it means it doesn't return any value
                                                                    //  "main" :  it is entry point of pragram, start program from here and it is called by runtime system.
                                                                    // "String arg[]" : it is used to command line argument.  
        int twoD[][]= new int[4][5];
        int k = 0, i, j;
        for (i = 0;  i< 4; i++)
        for (j = 0;  j< 5; j++){
            twoD[i][j] = k;
            k++;

        }
        for(i =0; i< 4; i++){
            for(j =0; j<5; j++)
            System.out.print(twoD[i][j]+" ");                
            System.out.println();                                   // System.out.Println():  it is used to print statement.
        }
    }
}

Explanation of this program:  

Key Components of the Program:

1. import java.util.*;
  • Imports all classes from the java.util package.
  • Not directly needed here as no utility classes (like Scanner) are used.
  • 2. Class Declaration: 


    class TwoDArray{


    • Defines the class TwoDArray. In Java, all code must reside inside a class.

    3. main Method:


        public static void main(String args[]){  
    • public: Makes the method accessible to the JVM.
    • static: Allows the method to run without creating an instance of the class.
    • void: Specifies that the method does not return any value.
    • String args[]: Allows the program to accept command-line arguments.

    4. Two-Dimensional Array Declaration:


       int twoD[][]= new int[4][5];


    • twoD is a 2D array with 4 rows and 5 columns.
    • Memory is allocated using new int[4][5].


    5. Looping the Array:

     
            int k = 0, i, j;
            for (i = 0;  i< 4; i++)
            for (j = 0;  j< 5; j++){
                twoD[i][j] = k;
                k++;
            }

    •  k starts at 0 and increments by 1 for each element.
    • This loop assigns values to each element of the 2D array row by row.

    6. Printing the Array:  

    • Outer loop (i) iterates over rows.
    • Inner loop (j) iterates over columns.
    • System.out.print prints values in the same line, separated by spaces.
    • System.out.println moves to the next line after printing each row.

    OUTPUT:


    PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\AllPraQue> java TwoDArray
    0 1 2 3 4
    5 6 7 8 9
    10 11 12 13 14
    15 16 17 18 19




    Post a Comment

    0 Comments