1

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}; 
3
  • 1
    You can just pass result1... Commented Mar 16, 2015 at 21:29
  • I don't get it. It seems like you are creating rowData to be identical to chessboard Commented Mar 16, 2015 at 21:30
  • JTable table = new JTable(result1, columnNames); i tried this but it doesnt seem to work? Commented Mar 16, 2015 at 21:36

1 Answer 1

1

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 Objects. See this relevant article for covariance in Java arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

oh right, i see i would pass the whole chessboard array. Im a newbie to Java so many thanks for this. I didnt know "An array of strings is also an array of Objects"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.