• Register
2 votes
29 views

Problem:

Hello programming people

I am very new to the world of programming, and I started with the language Java. Right now I am working with the array. I understand what array is and why it’s important in programming. But the problem is I only can handle a 1D-array so far.
Could any of you please help me out to write a program that prints a 2D array.
I must appreciate your kind help. Thanks

15.3k points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Hello Gavin,
It’s really an awesome feeling to know someone entered to this colorful environment of programming. You can write a 1D-array program already Congratulations!
Let me show you a way to print a 2D-array though it’s possible in many ways.

public class Print2DArray {
   public static void main(String[] args) {
      final int[][] myTwodArray = {
         { 1, 2, 3 },
         { 4, 5, 6 },
         { 7, 8, 9 }
      };
      for (int m = 0; m < myTwodArray.length; m++) {         //this is for the row.
         for (int n = 0; n < myTwodArray[m].length; n++) {   //this is for the coloumn.
            System.out.print(myTwodArray[m][n] + " ");
         }
         System.out.println();
      }
   }
}

I am using the for-loop to print a 2D array.  This is a very basic method and understandable for beginners.
Thanks.

94.3k points
edited by