如果将数组放入 map 中,您可以执行类似的操作。
public class Holder {
private Map<String, int[]> arrays = new HashMap<String, int[]>();
public Holder(String... names) {
for (String name : names) {
arrays.put(name, new int[1024]);
}
}
public void addShips(String fac, String type)
{
String arrayName = fac+"_"+type;
int[] array = arrays.get(arrayName);
...
}
}
我建议使用List<Integer>
,ArrayList<Integer>
而不是 int[]。
public class Holder {
private Map<String, List<Integer>> arrays = new HashMap<String, List<Integer>>();
public Holder(String... names) {
for (String name : names) {
arrays.put(name, new ArrayList<Integer>());
}
}
public void addShips(String fac, String type)
{
String arrayName = fac+"_"+type;
List<Integer> array = arrays.get(arrayName);
...
}
}