【问题标题】:java - Get and Set value in a map of listjava - 在列表的映射中获取和设置值
【发布时间】:2014-07-01 13:25:47
【问题描述】:

如何从插入到地图列表中的列表中检索值? 我有我的主要课程

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;
}
}

Occupancy是弧线不可用的时间,它有两个变量:initend

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); 

但它不起作用。我可以在我的弧列表中获取和设置值,但我不能使用我的占用列表。

【问题讨论】:

  • 例如:我有我的圆弧列表,我想存储origin=1和destination=13的arc占用时间。我想 setInit=0 和 setEnd=4。然后 setInit=5 和 setEnd=9...使用 "mapNode.get(arc.getOrigin()).get(arc.getDestination()).getOc​​cupancy().setInit(0);"它说:java.lang.NullPointerException

标签: java list map


【解决方案1】:

像这样初始化泛型:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多