Umesh Kacha
Umesh Kacha

Reputation: 13676

How to create 2D array of Object from a List?

I would like to convert ArrayList into 2D array of Objects. List is something like

[[AD,ADCB, N], [AFC, Fund, Y], [IO, dfdfdfd, N]]

I would like to convert this List into array of objects and I would like to modify Y, N field into Boolean Values someting like

Object[] rowdata = {
    {AD, ADCB, Boolean.FALSE}, 
    {AFC, Fund, Boolean.TRUE}, 
    {IO, dffdfdf, Boolean.FALSE}}

after that I can stuff into JTable model and those boolean values will be visible as JCheckboxes.

What should be the best way to convert this list into 2D array of objects so that I can pass into JTable TableModel?

Upvotes: 0

Views: 1422

Answers (1)

christopher
christopher

Reputation: 27346

In your example, you show that there are three members of each object that you want to store. So, if N is the number of items in your arraylist, you need a multidimensional array of N * 3. ie:

Object[][] table = new Object[list.size()][3];

Then you're going to use a for loop, to cycle through each object in the list:

for(int x = 0; x < list.size(); x++)
{
     Object currentObject = list.get(x);
     table[x][0] = currentObject.getValue();
     // Load the first value.
     ...
     table[x][2] = currentObject.getYorN().equalsIgnoreCase("Y")? true:false;
     // Embed a selection statement to decide if to store true or false.
}

Upvotes: 1

Related Questions