1

I would like to populate a JTable with the cnstructor like this :

new JTable(datas, columns);

so I need an Object[] for the columns and a Object[][] for the datas. The problem is when I try to get the datas.

In one of my method I work with an ArrayList<ArrayList<Object>> and I would like to cast it to an Object[][].

Here's a sample of my code :

ArrayList<ArrayList<Object>> datas = new ArrayList<ArrayList<Object>>();

for (Map.Entry<Integer, JSObject> entry : params.entrySet()) {
    datas.add(entry.getValue().getValues()); // getValues returns an ArrayList<Object>
}

return new datas.toArray(); //  not seems to work

I saw in this question Convert ArrayList<Object[]> to Object[][] some idea, but I'm not sure about the result.

Thanks in advance !

3
  • Why don't you use Object[][] directly for datas? Commented Mar 30, 2014 at 11:51
  • Also, return new datas.toArray(); is not a valid statement (new should probably be removed) Commented Mar 30, 2014 at 11:53
  • because I get params from another object where I prefere to use ArrayList but I try this solution, and let you know what comes from it... And yes the new had nothing to do here, thanks Commented Mar 30, 2014 at 11:54

1 Answer 1

4

Can't you just change the type of datas to an ArrayList<Object[]>, and use toArray() each time you add an element:

ArrayList<Object[]> datas = new ArrayList<Object[]>();
for (Map.Entry<Integer, JSObject> entry : params.entrySet()) {
    datas.add(entry.getValue().getValues().toArray());
}
return datas.toArray(new Object[][]{});
Sign up to request clarification or add additional context in comments.

2 Comments

I have to add (Object[][]) to cast the return statment but it seems to work, I test it and let you know
@Nobe4: True, used the other toArray() instead to prevent the need to cast.

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.