How to print an 2d array of Char (JAVA) [duplicate]
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));
}
}
java arrays
marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel
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.
add a comment |
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));
}
}
java arrays
marked as duplicate by GBlodgett, azro, user6655984, Mark Rotteveel
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
add a comment |
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));
}
}
java arrays
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
java arrays
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
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
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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();
}
1
No; this isn't legal. You need to provide the type tofor-each
loops.
– Elliott Frisch
Nov 11 at 23:22
add a comment |
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");
}
add a comment |
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?
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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();
}
1
No; this isn't legal. You need to provide the type tofor-each
loops.
– Elliott Frisch
Nov 11 at 23:22
add a comment |
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();
}
1
No; this isn't legal. You need to provide the type tofor-each
loops.
– Elliott Frisch
Nov 11 at 23:22
add a comment |
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();
}
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();
}
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 tofor-each
loops.
– Elliott Frisch
Nov 11 at 23:22
add a comment |
1
No; this isn't legal. You need to provide the type tofor-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
add a comment |
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");
}
add a comment |
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");
}
add a comment |
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");
}
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");
}
answered Nov 11 at 23:22
Tim Hunter
915
915
add a comment |
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
answered Nov 11 at 23:27
ToTheMax
1328
1328
add a comment |
add a comment |
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