Thursday, July 30, 2009

I need help with this java programming question?

I'm suppose to declare and intialize an arrayList of PlanetS with the name solarSystem to represent our solar system. The arrayList sholud contain one element for each planet in our solar system.

I need help with this java programming question?
ArrayLists can contain any object type, as well as a mixture of object types. As such, populating the ArrayList is easy:





ArrayList solarSystem = new ArrayList();


//create a PlanetS object for Venus


solarSystem.add(/*name of PlanetS object*/);


//create a PlanetS object for Mercury


solarSystem.add(/*name of PlanetS object*/);


//etc etc...





The complexity comes when you want to get data out of the ArrayList, as you will have to cast it to the correct type:





PlanetS p = (PlanetS) solarSystem.get(1);


No comments:

Post a Comment