How to print an 2d array of Char (JAVA) [duplicate]












0















This question already has an answer here:




  • What's the simplest way to print a Java array?

    31 answers




I am trying to print out a gameBoard that has a "-" for each spot of the array: however every time I run this code I get this printed to the console:




[[C@2a139a55.




Any suggestions?



public class Game {

public static void main(String args){

char realBoard = new char[7][7];

for (int i=0;i<7;i++){
for(int j=0;j<7;j++){
realBoard[i][j]='-';
}
}
System.out.print((realBoard));
}
}









share|improve this question















marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 17:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
    – Elliott Frisch
    Nov 11 at 23:21
















0















This question already has an answer here:




  • What's the simplest way to print a Java array?

    31 answers




I am trying to print out a gameBoard that has a "-" for each spot of the array: however every time I run this code I get this printed to the console:




[[C@2a139a55.




Any suggestions?



public class Game {

public static void main(String args){

char realBoard = new char[7][7];

for (int i=0;i<7;i++){
for(int j=0;j<7;j++){
realBoard[i][j]='-';
}
}
System.out.print((realBoard));
}
}









share|improve this question















marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 17:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
    – Elliott Frisch
    Nov 11 at 23:21














0












0








0








This question already has an answer here:




  • What's the simplest way to print a Java array?

    31 answers




I am trying to print out a gameBoard that has a "-" for each spot of the array: however every time I run this code I get this printed to the console:




[[C@2a139a55.




Any suggestions?



public class Game {

public static void main(String args){

char realBoard = new char[7][7];

for (int i=0;i<7;i++){
for(int j=0;j<7;j++){
realBoard[i][j]='-';
}
}
System.out.print((realBoard));
}
}









share|improve this question
















This question already has an answer here:




  • What's the simplest way to print a Java array?

    31 answers




I am trying to print out a gameBoard that has a "-" for each spot of the array: however every time I run this code I get this printed to the console:




[[C@2a139a55.




Any suggestions?



public class Game {

public static void main(String args){

char realBoard = new char[7][7];

for (int i=0;i<7;i++){
for(int j=0;j<7;j++){
realBoard[i][j]='-';
}
}
System.out.print((realBoard));
}
}




This question already has an answer here:




  • What's the simplest way to print a Java array?

    31 answers








java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:22









AS Mackay

1,9094816




1,9094816










asked Nov 11 at 23:16









Msarn21

34




34




marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 17:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 17:03


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
    – Elliott Frisch
    Nov 11 at 23:21


















  • System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
    – Elliott Frisch
    Nov 11 at 23:21
















System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
– Elliott Frisch
Nov 11 at 23:21




System.out.println(Arrays.deepToString(realBoard)); also, for (int i = 0; i < realBoard.length; i++) { Arrays.fill(realBoard[i], '-'); }
– Elliott Frisch
Nov 11 at 23:21












3 Answers
3






active

oldest

votes


















2














realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again



for(char y: realBoard) {
for(char x: realBoard) {
System.out.print(x);
}
System.out.println();
}





share|improve this answer



















  • 1




    No; this isn't legal. You need to provide the type to for-each loops.
    – Elliott Frisch
    Nov 11 at 23:22



















0














Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.



for(int i = 0; i < 7; i++) {
for(int j = 0; j < 7; j++) {
//Print for each row
System.out.print("-");
}
//Move to next line
System.out.print("n");
}





share|improve this answer





























    0














    You can't print a 2D array like that. To print a 2D array in one line you can use:



    System.out.println(Arrays.deepToString(realBoard));


    Or in multiple lines:



    for(char x: realBoard)
    System.out.println(Arrays.toString(x));


    Credits: Java - Best way to print 2D array?






    share|improve this answer




























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again



      for(char y: realBoard) {
      for(char x: realBoard) {
      System.out.print(x);
      }
      System.out.println();
      }





      share|improve this answer



















      • 1




        No; this isn't legal. You need to provide the type to for-each loops.
        – Elliott Frisch
        Nov 11 at 23:22
















      2














      realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again



      for(char y: realBoard) {
      for(char x: realBoard) {
      System.out.print(x);
      }
      System.out.println();
      }





      share|improve this answer



















      • 1




        No; this isn't legal. You need to provide the type to for-each loops.
        – Elliott Frisch
        Nov 11 at 23:22














      2












      2








      2






      realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again



      for(char y: realBoard) {
      for(char x: realBoard) {
      System.out.print(x);
      }
      System.out.println();
      }





      share|improve this answer














      realBoard is an array, an object, so you can't just print it like that. You will need to iterate over the elements again



      for(char y: realBoard) {
      for(char x: realBoard) {
      System.out.print(x);
      }
      System.out.println();
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 11 at 23:24

























      answered Nov 11 at 23:22









      MegaBluejay

      35118




      35118








      • 1




        No; this isn't legal. You need to provide the type to for-each loops.
        – Elliott Frisch
        Nov 11 at 23:22














      • 1




        No; this isn't legal. You need to provide the type to for-each loops.
        – Elliott Frisch
        Nov 11 at 23:22








      1




      1




      No; this isn't legal. You need to provide the type to for-each loops.
      – Elliott Frisch
      Nov 11 at 23:22




      No; this isn't legal. You need to provide the type to for-each loops.
      – Elliott Frisch
      Nov 11 at 23:22













      0














      Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.



      for(int i = 0; i < 7; i++) {
      for(int j = 0; j < 7; j++) {
      //Print for each row
      System.out.print("-");
      }
      //Move to next line
      System.out.print("n");
      }





      share|improve this answer


























        0














        Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.



        for(int i = 0; i < 7; i++) {
        for(int j = 0; j < 7; j++) {
        //Print for each row
        System.out.print("-");
        }
        //Move to next line
        System.out.print("n");
        }





        share|improve this answer
























          0












          0








          0






          Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.



          for(int i = 0; i < 7; i++) {
          for(int j = 0; j < 7; j++) {
          //Print for each row
          System.out.print("-");
          }
          //Move to next line
          System.out.print("n");
          }





          share|improve this answer












          Unless you need to use the array data of mark elsewhere, you would be better off just using print statements inside your loops.



          for(int i = 0; i < 7; i++) {
          for(int j = 0; j < 7; j++) {
          //Print for each row
          System.out.print("-");
          }
          //Move to next line
          System.out.print("n");
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 23:22









          Tim Hunter

          915




          915























              0














              You can't print a 2D array like that. To print a 2D array in one line you can use:



              System.out.println(Arrays.deepToString(realBoard));


              Or in multiple lines:



              for(char x: realBoard)
              System.out.println(Arrays.toString(x));


              Credits: Java - Best way to print 2D array?






              share|improve this answer


























                0














                You can't print a 2D array like that. To print a 2D array in one line you can use:



                System.out.println(Arrays.deepToString(realBoard));


                Or in multiple lines:



                for(char x: realBoard)
                System.out.println(Arrays.toString(x));


                Credits: Java - Best way to print 2D array?






                share|improve this answer
























                  0












                  0








                  0






                  You can't print a 2D array like that. To print a 2D array in one line you can use:



                  System.out.println(Arrays.deepToString(realBoard));


                  Or in multiple lines:



                  for(char x: realBoard)
                  System.out.println(Arrays.toString(x));


                  Credits: Java - Best way to print 2D array?






                  share|improve this answer












                  You can't print a 2D array like that. To print a 2D array in one line you can use:



                  System.out.println(Arrays.deepToString(realBoard));


                  Or in multiple lines:



                  for(char x: realBoard)
                  System.out.println(Arrays.toString(x));


                  Credits: Java - Best way to print 2D array?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 23:27









                  ToTheMax

                  1328




                  1328















                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()