Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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
    •