java-在列表的映射中获取和设置值
如何从插入地图列表的列表中检索值? 我有主课java-在列表的映射中获取和设置值,java,list,map,Java,List,Map,如何从插入地图列表的列表中检索值? 我有主课 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
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);
}
}
}
}
我无法在我的入住清单中存储价值。我试过:
mapNode.get(arc.getOrigin()).get(arc.getDestination()).getOccupancy().setInit(0);
但它不起作用。我可以在我的arc列表中获取和设置值,但我不能使用我的占用列表 初始化泛型,如下所示:
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>());
}
私有地图映射节点;
私人最终弧;
公共路径(){
this.arc=新圆弧();
双coll[][]={{1,13},{2,21},{21,2},{7,21},{21,5};
mapNode=newHashMap();
for(int i=0;i
之后,您可以轻松地索引和获取类型化项,而不必强制转换对象
当然,您可以从如下列表中获得列表和项目:
Map<Integer, List<Arc>> mapNode = ...;
...
List<Arc> list = mapNode.get(5); //example Integer
Arc arc = list.get(0); //index of 0
MapMapNode=。。。;
...
List List=mapNode.get(5);//整数示例
Arc=list.get(0);//0的索引
例如:我有我的弧列表,我想存储原点为1、终点为13的弧的占用时间。我想设置init=0和setEnd=4。然后设置init=5和setEnd=9…使用“mapNode.get(arc.getOrigin()).get(arc.getDestination()).getoccupation().setInit(0);”它说:java.lang.NullPointerException
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>());
}
Map<Integer, List<Arc>> mapNode = ...;
...
List<Arc> list = mapNode.get(5); //example Integer
Arc arc = list.get(0); //index of 0