Reputation: 13
So I have these four arrays
ArrayList<String> names = new ArrayList<>();
ArrayList<String> licensePlates = new ArrayList<>();
ArrayList<Integer> mileage = new ArrayList<>();
ArrayList<Integer> engineHours = new ArrayList<>();
But returning them one by one is a hassle so I was wondering how I would go about making a multi-dimensional array to hold all these values instead. And also how I would go about adding something to them, for example, let's say I wanted to add an extra licence plate to the licence plate part of the multi-dimensional array.
Upvotes: 0
Views: 54
Reputation: 307
I would highly recommend louis-wasserman approach of creating a class. If you are looking for an alternative then you can take a look at Guava's ListMultiMap
ListMultiMap<String, Object> multiDimArray = ArrayListMultiMap.create();
multiDimArray.put("names", "merc");
multiDimArray.put("mileage", 10);
multiDimArray.put("names", "bmw");
multiDimArray.put("mileage", 20);
// To retrieve the names lists:
List<String> names = multiDimArray.get("names");
Upvotes: 0
Reputation: 198481
Make a class. It is really a bad idea to try to shoehorn lists with different meanings into one big multidimensional list, or to try to have multiple lists "line up" like that.
class Car {
String name;
String licensePlate;
int mileage;
int engineHours;
...constructor, getters...
}
List<Car> carList;
Upvotes: 2