Converting a row from a matrix to a String
I'm trying to convert a row from a matrix of Char's to a String and all I've been able to get is a bunch of memory locations. I was hoping someone can lend me a hand. Here's what I've got:
for(int c=0; c<tempBoard.length; c++){
String charArray = " ";
for(int v=0; v<tempBoard.length; v++){
board[c][v] = tempBoard[c][v];
charArray = tempBoard[c].toString();
}
System.out.println(charArray);
}
Encrypted
Junior Poster in Training
77 posts since Feb 2010
Reputation Points: 10
Solved Threads: 4
Calling a String 'charArray' is pretty confusing to say the least. Anyway, here is an example to clear things up. This puts the first rows of chars into the variable temp.
char[][] chars = new char[10][10];
//Put some stuff in the 2d array
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
chars[i][j] = (char) (i*j);
// 0 is the row, i is the column
String temp = "";
for (int i = 0; i < 10; i++) temp+=chars[0][i];
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Thanks, that should do it.. not sure why I didn't think of that. About the variable.. yeh I know, I'm too used to coding in C and that happened to be the first thing that crossed my mind.
Calling a String 'charArray' is pretty confusing to say the least. Anyway, here is an example to clear things up. This puts the first rows of chars into the variable temp.
char[][] chars = new char[10][10];
//Put some stuff in the 2d array
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
chars[i][j] = (char) (i*j);
// 0 is the row, i is the column
String temp = "";
for (int i = 0; i < 10; i++) temp+=chars[0][i];
Encrypted
Junior Poster in Training
77 posts since Feb 2010
Reputation Points: 10
Solved Threads: 4