So I have this ArrayList
filled with Objects and I need to convert it to an Object[][]
to put it easily in a JTable
.
Example :
I have an ArrayList<Animal>
:
class Animal{
String color;
int age;
String eatsGrass;
// Rest of the Class (not important)
}
What I want from this is a JTable with the following column names :
Color - Age - Eats Grass?
My current method looks like this :
List<Animal> ani = new ArrayList();
// Fill the list
Object[][] arrayForTable = new Object[ani.size()][3];
for (int i = 0 ; i < ani.size() ; i++){
for (int j = 0 ; j < 3 ; j++){
switch(j){
case 1 : arrayForTable[i][j] = ani.get(j).getColor();break;
case 2 : arrayForTable[i][j] = ani.get(j).getAge();break;
default : arrayForTable[i][j] = ani.get(j).getEatsGrass();break;
}
}
}
It works fine but is there an easier way to make this possible. I can not imagine myself using the same method for a JTable
with 25 columns for example.