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

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.

13 9 6
94,240 points

Related questions

0 votes
1 answer 36 views
36 views
Problem: I'm a beginner in Java, and I need to write a next() method, which will return current value from 2D array and move pointer to the next element. It's similar to Iterators's next() method. For example, we may have: int[][] values = {{1, 2}, {3, 4}}; When we ... at 2), second use - 2(+move of pointer), third - 3(+move pointer), etc. I wrote some bad code to illustrate what I want to do:
asked Dec 5, 2020 Mashhoodch 13k points
1 vote
1 answer 335 views
335 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, 2020 Gavin 15.3k points
0 votes
1 answer 16 views
16 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, 2020 game 4.6k points
0 votes
1 answer 2 views
2 views
Problem: I am doing a c++ project someone help me to pass 2d array to function in cpp.
asked Apr 1 zayed1 24.7k points
0 votes
1 answer 15 views
15 views
Problem: In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function. Before you learn about passing arrays as a function argument Let's see an example, int total(int marks[5]) { // code }
asked Dec 15, 2020 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: Why this happened: How To Solve "valueerror: expected 2d array, got 1d array instead:"
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
2 answers 97 views
97 views
Please how do I solve valueerror: expected 2d array, got 1d array instead: ? Thanks
asked Oct 23, 2020 psandprop 2.4k points
0 votes
1 answer 2 views
2 views
Problem How to declare 2dimentional array in c++
asked Apr 1 Subhendu 4.1k points
0 votes
1 answer 3 views
3 views
Problem Anyone can help me, how to pass 2d array from function in c++
asked Mar 24 Subhendu 4.1k points
0 votes
1 answer 12 views
12 views
How to dynamically allocate a 2d array in c? What are the different ways to achieve allocation?
asked Oct 9, 2020 sikandar 2.4k points