0

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);
1
  • if your problem solved please mark one of the best answers as accepted... Commented Jun 7, 2013 at 9:41

2 Answers 2

3

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.

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

2 Comments

i tried above code and it worked for me.. thnax for ur help buddy
@NimeshMadushan: Always glad to help :) If this answer helped you solve your problem, please mark it as "accepted", so users facing a similar problem in the future will be able to spot it easily.
2

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.

2 Comments

Am I understanding something wrong? To me, it looks as if this will create the data 2d-array multiple times and with only one row, and all except the last instance will be thrown away.
Yes, that's why I said I don't get the reason for using a loop.

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.