• Register
2 votes
15 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

12 7 7
15,250 points

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.

13 9 6
94,240 points

Related questions

1 vote
1 answer 30 views
30 views
Problem: I&rsquo;ve completed my basic java course yesterday. My instructor provided me a problem to solve as an assignment for the final test. The problem is to make a tic tac toe java code using a 2d array. I know how to use a 2d array and handle it. But I ... not looking for the whole solution, but yeah if you can provide it might help me a lot to understand the process better. Thanks, fellow!
asked Mar 31 Gavin 15.3k points
0 votes
1 answer 12 views
12 views
I have a function that I want to take, as a parameter, a variable size 2D array. So far I have this: void myFunction (double ** myArray) { myArray [x] [y] = 5; etc... } And I have showed an array in my code: double anArray [10] [10]; However, ... changes made to my Function should alter the state of an Array. If I understand correctly, I just want to pass as argument a pointer to a 2D array.
asked Aug 8 game 4.6k points
0 votes
2 answers 23 views
23 views
Please how do I solve valueerror: expected 2d array, got 1d array instead: ? Thanks
asked Oct 23 psandprop 2.4k points
0 votes
1 answer 7 views
7 views
How to dynamically allocate a 2d array in c? What are the different ways to achieve allocation?
asked Oct 9 sikandar 2.4k points
1 vote
1 answer 39 views
39 views
Problem: Hi all, I am a beginner in C++ programming. I am reading and trying to learn the C++ functions. Let say, I have a two-dimensional array and I want the values to be printed via a function call. How to do that in C++? I&rsquo;ve given a ... format to do that? If not, please share your suggestions and ideas with me. I am looking forward to reading your comments eagerly. Thanks, guys.
asked May 10 adamSw 11.3k points