Reputation: 674
i am trying to put the results of an array into a new Object array but cant seem to work out how.
I first create a chessboard array that is 10*10 and put the word hello into all the elements.
I then create a loop to go through all the elements to create a 10*10 matrix of what my array holds, in my case "hello". The output is called result1
I now want to put all the elements of result1 into an Object array called rowData[][]. The array of this will go into into a Jtable
JTable table = new JTable(rowData, columnNames);
String [][] chessboard = new String[10][10];
for (int row = 0;row<=9;row++){
for (int col = 0; col <=9; col++){
chessboard[row][col] = "hello";
}
}
String result1 = "";
for (int row1 = 0;row1<=9;row1++){
for (int col2 = 0; col2 <=9; col2++){
result1 += chessboard[row1][col2];
}
result1 += "\r\n";
}
System.out.format(result1);
Object rowData[][] = {the result1 into each element of the new Object array};
Upvotes: 1
Views: 66
Reputation: 72844
You can simply pass the String
array as it is -- no need for a new Object[][]
:
JTable table = new JTable(chessboard, columnNames);
An array of strings is also an array of Object
s. See this relevant article for covariance in Java arrays.
Upvotes: 1