如何从插入地图列表中的列表中检索值? 我有我的主要课程
public class Path {
private Map<Integer, List<Arc>> mapNode;
private final Arc arc;
public Path() {
this.arc = new Arc();
double coll[][] = {{1, 13}, {2, 21}, {21, 2}, {7, 21}, {21, 5}};
mapNode = new HashMap();
for (int i = 0; i < coll.length; i++) {
if (!mapNode.containsKey((int) coll[i][0])) {
mapNode.put((int) coll[i][0], new ArrayList());
}
Arc arc = new Arc();
arc.setOrigin((int) coll[i][0]);
arc.setDestination((int) coll[i][1]);
mapNode.get(arc.getOrigin()).add(arc);
mapNode.get(arc.getOrigin()).get(arc.getDestination()).getOccupancy().setInit(0);
}
}
}
在我的地图中,我有一个Arc列表。它包含来源,目的地以及占用列表:
public class Arc {
private int origin;
private int destination;
private Occupancy occupancy;
private List <Occupancy> time_occupancy;
public Arc(){
}
public Arc(int origin, int destination){
this.origin = origin;
this.destination = destination;
}
public Occupancy getOccupancy() {
return occupancy;
}
public void setOccupancy(Occupancy occupancy) {
this.occupancy = occupancy;
}
public List<Occupancy> getTime_occupancy() {
return time_occupancy;
}
public void setTime_occupancy(List<Occupancy> time_occupancy) {
this.time_occupancy = time_occupancy;
}
public int getOrigin() {
return origin;
}
public void setOrigin(int origin) {
this.origin = origin;
}
public int getDestination() {
return destination;
}
public void setDestination(int destination) {
this.destination = destination;
}
}
占用是弧不可用的时间,它有两个变量:init
和end
。
class Occupancy {
double init;
double end;
public Occupancy(double init, double end){
this.init = init;
this.end = end;
}
public double getInit() {
return init;
}
public void setInit(double init) {
this.init = init;
}
public double getEnd() {
return end;
}
public void setEnd(double end) {
this.end = end;
}
}
我不能在我的入住名单中存储价值。我试过了:
mapNode.get(arc.getOrigin()).get(arc.getDestination()).getOccupancy().setInit(0);
但它不起作用。我可以在我的弧列表中获取并设置值,但我不能使用我的占用列表。
答案 0 :(得分:0)
初始化泛型如下:
private Map<Integer, List<Arc>> mapNode;
private final Arc arc;
public Path() {
this.arc = new Arc();
double coll[][] = {{1, 13}, {2, 21}, {21, 2}, {7, 21}, {21, 5}};
mapNode = new HashMap<Integer, List<Arc>>();
for (int i = 0; i < coll.length; i++) {
if (!mapNode.containsKey((int) coll[i][0])) {
mapNode.put((int) coll[i][0], new ArrayList<Arc>());
}
之后,您可以轻松索引并获取TYPED项目,而不必投射对象。
当然,您可以从以下列表中获取列表和项目:
Map<Integer, List<Arc>> mapNode = ...;
...
List<Arc> list = mapNode.get(5); //example Integer
Arc arc = list.get(0); //index of 0