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.
但是一个接一个地返回它们很麻烦所以我想知道如何制作一个多维数组来代替所有这些值。还有我如何向它们添加一些内容,例如,假设我想在多维数组的牌照部分添加一个额外的牌照。
2 个解决方案
#1
2
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;
#2
0
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
我强烈推荐louis-wasserman创建课程的方法。如果您正在寻找替代方案,那么您可以查看Guava的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");
#1
2
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;
#2
0
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
我强烈推荐louis-wasserman创建课程的方法。如果您正在寻找替代方案,那么您可以查看Guava的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");