我有一个位置POJO,它存储从JSON文件中解析出来的位置对象,我想将其映射到一个图形。图中每个节点的位置对应于它的id字段,其中id =“1”是开始节点,id =“10”是目标节点。如何迭代对象列表并分配子对象?
为了解决这个问题,我调整了一个Node类以包含诸如setWeight()
,addChildLocation()
等方法,但我不确定如何从我的位置列表创建图形。我知道如何通过硬编码位置的,并呼吁addChildren,通过执行以下操作来创建图形,但不知道如何从已有的位置对象的列表创建:
Node LocationOne= new Node("LocationOne", 170);
LocationOne.addChildNode(LocationTwo, 105);
我对这个问题的看法,是否应该使用循环来遍历列表中的位置,并将每个位置添加为前一个的子项。
基本上,我想知道如何遍历位置对象的列表,并在每个顺序位置调用addChild?
下面是我使用的位置对对象表示映射位置类:
public class Location {
private Location[] location;
private int id;
private String description;
private String weight;
private String name;
private Exit[] exit;
private boolean visited = false;
private boolean goalLocation;
private int approximateDistanceFromGoal = 0;
private Location parent;
private Map<Location, Integer> children = new HashMap<Location, Integer>();
public Location() {
super();
}
public Location(String name){
this.name = name;
}
public Location(String name, int goalDistance){
this.name = name;
this.approximateDistanceFromGoal = goalDistance;
}
public Location[] children(){
return (Location[]) children.keySet().toArray(new Location[children.size()]);
}
public int getDistance(Location loc){
if(children.get(loc) == null) System.out.println(this.name + ": " + loc.getName());
return children.get(loc);
}
public int getChildLocationCount(){
return children.size();
}
public void addChildLocation(Location child, int distance){
children.put(child, distance);
}
public boolean isLeaf(){
if (children.size() > 0){
return false;
}else{
return true;
}
}
public void removeChild(Location child){
children.remove(child);
}
public Location[] getLocation() {
return location;
}
public void setLocation(Location[] location) {
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getName()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public Exit[] getExit() {
return exit;
}
public void setExit(Exit[] exit) {
this.exit = exit;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public boolean isGoalLocation() {
return goalLocation;
}
public void setGoalLocation(boolean goalLocation) {
this.goalLocation = goalLocation;
}
public int getApproximateDistanceFromGoal() {
return approximateDistanceFromGoal;
}
public void setApproximateDistanceFromGoal(int approximateDistanceFromGoal) {
this.approximateDistanceFromGoal = approximateDistanceFromGoal;
}
public Location getParent() {
return parent;
}
public void setParent(Location parent) {
this.parent = parent;
}
@Override
public String toString() {
return "Location [location=" + Arrays.toString(location) + ", id=" + id
+ ", description=" + description + ", weight=" + weight
+ ", name=" + name + ", exit=" + Arrays.toString(exit)
+"]";
}
}
我会创建一个HashTable,键是位置和值是连接到键的所有位置的列表。 – Kunal 2015-04-02 00:58:38
我不会构建自己管理节点的工具,而是我可能会使用图形数据库。结帐http://neo4j.com或http://thinkaurelius.github.io/titan/。 – 2015-04-02 01:03:41
@Kinsley我已经在Location类中创建了一个映射,并且使用了一个方法addChild来获取位置名称和int ID,我的问题是如何将addChild调用到对象列表中的每个后续位置对象以创建图形。 – 2015-04-02 01:23:08