I have a array list of objects (<ArrayList> Cars
) which contains two single elements. These elements are New Car Model[name=Prius, manufacturer=Toyota, year=2001]
and New Car Model[name=Panda, manufacturer=Fiat, year=2005;]
. I would like to create a two dimensional String array (in this case String [2][3] Cars
] where to each car would correspond a set of attributes (e.g. Cars[0][2] = "2001"
). Considering that I can't change the content in the ArrayList, what methods should I use in order to create such ordered 2D array? String.Split()? What else?
views:
101answers:
1
A:
Is the data in the New Car Model class in the form of a String, or is it 3 separate fields?
If it's just one string, use the .split(", ")
method. It returns an array of strings leaving out the characters you used to split it by. You could just assign that to each new array.
for(i = 0; i < 2; i++)
Cars[i] = carModel.split(", ");
otherwise just assign it to each member of the second array manually, then loop through the first array.
for(i = 0; i < 2; i++)
{
Cars[i][0] = carModel.getName()
Cars[i][1] = carModel.getManufacturer()
Cars[i][2] = carModel.getDate()
}
WLPhoenix
2010-02-05 23:14:23