Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Way to print a 2D array.

1. ## Way to print a 2D array.

Hi

This is a simple way for writing a 2D array :-

int twoD[][]={{0,1,2,3,4},{5,6,7,8,9}};

But problem is how can i print it ?

I tried system.out.println(twoD [] [] ), but it gave error

Any help, i want to print the whole array.

• If you mean to print out the array id, then you can simply use `System.out.println(twoD);`. If you intend to actually show what is in them, use a collection or write a loop / recursive method to display the items within the array.

• Originally Posted by Fou-Lu
If you mean to print out the array id, then you can simply use `System.out.println(twoD);`. If you intend to actually show what is in them, use a collection or write a loop / recursive method to display the items within the array.
i tried printing (twoD) and the output was : "[[I@19821f " << can you please tell me how ?

also can you please mention the loop ? actually we have this program in our exams and its really frustrating at the method our sir taught us so wanna go for simpler one.

• As I said, the output of a print is that of the memory identifier.
And what method were you shown? I'm willing to bet its either a collection conversion or that of looping as is. Printing takes either the use of a primitive scalar, or a .toString() on an object. Since an array of primitive is a complex type, you cannot just print it directly.

• Originally Posted by Fou-Lu
As I said, the output of a print is that of the memory identifier.
And what method were you shown? I'm willing to bet its either a collection conversion or that of looping as is. Printing takes either the use of a primitive scalar, or a .toString() on an object. Since an array of primitive is a complex type, you cannot just print it directly.
we had to print 2D array program such as :-

0 1 2 3 4
5 6 7 8 9
10 11 13 13 14
15 16 17 18 19

so its like 4 rows and 5 colums, their were total 4 for loops in program, the first 2 were to assign values ( 0 - 19 ) with help of variable, and other 2 for loop was used to print the variables.

• Assignment is irrelevant since you need to populate them somehow.
2 loops for a 2d array is what you need to do. Each level is another loop. Recursion is the other option since then you don't need to concern yourself with the depth.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•