简体   繁体   中英

Error in declaring and assigning to string array

I want to declare an array out side the for loop and assign values to the string array. but i am getting an error. please give me a suggestion to do this. my code is given below.

    String[][] data=null; 

    for (int x = 0; x < dtm.getRowCount(); x++) {
            data  = {{"sds","sdsds"}}; <<< im getting error in here.
    }
    DefaultTableModel model = new DefaultTableModel(data, headers);

Try it like this:

String[][] data = new String[dtm.getRowCount()][];
for (int x = 0; x < dtm.getRowCount(); x++) {
    data[x] = new String[]{"sds", "sdsds"};
}
DefaultTableModel model = new DefaultTableModel(data, headers);

See, also, this short demo .

You're declaring the array in the wrong way. It should be:

 for (int x = 0; x < dtm.getRowCount(); x++) {
     data  = new String[][]{new String[]{"sds","sdsds"}};
 }

Anyway, with the code provided in the question, I don't get the reason for using a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM