Multiple Values Per Element in Java Array

Ask by : hasMobi - Android Apps November 04, 2012 16:05

I was wondering if there is a way to store more than 2 elements of information per item in an array of items.

For example, for an array of Cities I would like to store Population (int), Name (String), Mayor (String), Is there an airport (boolean).

I need a modifiable list, e.g. one where I can add and access elements easily by their position.

View original question

Answer by : JB NizetNovember 04, 2012 15:03

Create a class called City, and containing the necessary fields: population, name, mayor, airportPresent.

Use a java.util.List<City> (java.util.ArrayList being usually the best choice as an implementation).

Java is an OO language. Use objects to store your information, and encapsulate behavior. Using multi-dimensional arrays is not the appropriate solution.

View original answer