Java 如何遍历对象列表并分配子对象?
我有一个Location POJO,它存储从JSON文件解析的Location对象,我想将其映射到图形。图中每个节点的位置对应于其id字段,其中id=“1”是开始节点,id=“10”是目标节点 为了解决这个问题,我修改了一个节点类,使其包含诸如Java 如何遍历对象列表并分配子对象?,java,object,graph-algorithm,pojo,java-5,Java,Object,Graph Algorithm,Pojo,Java 5,我有一个Location POJO,它存储从JSON文件解析的Location对象,我想将其映射到图形。图中每个节点的位置对应于其id字段,其中id=“1”是开始节点,id=“10”是目标节点 为了解决这个问题,我修改了一个节点类,使其包含诸如setWeight()、addChildLocation()等方法,但我不确定如何从位置列表创建图形。我知道如何通过硬编码位置并调用addChildren来创建图形,方法是执行以下操作,但不确定如何从已可用位置对象列表中创建图形: Node Loc
setWeight()
、addChildLocation()
等方法,但我不确定如何从位置列表创建图形。我知道如何通过硬编码位置并调用addChildren来创建图形,方法是执行以下操作,但不确定如何从已可用位置对象列表中创建图形:
Node LocationOne= new Node("LocationOne", 170);
LocationOne.addChildNode(LocationTwo, 105);
我对这个问题的看法是,for..each
循环应该用于循环列表中的位置,并将每个位置添加为前一个的子位置
基本上,我想知道如何遍历Location对象的列表,并在每个连续的位置上调用addChild
下面是我用来将位置映射到对象表示的Location类:
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)
+"]";
}
}
公共类位置{
私人地点[]地点;
私有int-id;
私有字符串描述;
私有字符串权重;
私有字符串名称;
私人出口[]出口;
私有布尔值=false;
私有布尔分配;
private int approximatedInstanceFromGoal=0;
私人位置家长;
private-Map-children=new-HashMap();
公共场所(){
超级();
}
公共位置(字符串名称){
this.name=名称;
}
公共位置(字符串名称,整数目标位置){
this.name=名称;
这一点。近似的标准分数=目标分数;
}
公共场所[]儿童(){
返回(位置[])children.keySet().toArray(新位置[children.size()]);
}
公共距离(位置loc){
if(children.get(loc)==null)System.out.println(this.name+”:“+loc.getName());
返回儿童。获取(loc);
}
public int getChildLocationCount(){
返回children.size();
}
公共void addChildLocation(位置子项,整数距离){
儿童。放置(儿童,距离);
}
公共布尔isLeaf(){
if(children.size()>0){
返回false;
}否则{
返回true;
}
}
public void removeChild(位置子项){
儿童。移除(儿童);
}
公共位置[]getLocation(){
返回位置;
}
公共无效设置位置(位置[]位置){
这个位置=位置;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getDescription()
{
返回说明;
}
公共void集合描述(字符串描述)
{
this.description=描述;
}
公共字符串getWeight(){
返回重量;
}
公共空隙设置重量(管柱重量){
重量=重量;
}
公共字符串getName()
{
返回名称;
}
公共void集合名(字符串名)
{
this.name=名称;
}
公共出口[]getExit(){
返回出口;
}
public void setExit(出口[]出口){
this.exit=退出;
}
公共布尔值(){
回访;
}
已访问的公共void集合(已访问的布尔值){
这是参观过的;
}
公共布尔值isGoalLocation(){
返回目标分配;
}
public void setGoalLocation(布尔goalLocation){
this.goalLocation=goalLocation;
}
public int getApproximatedInstanceFromGoal(){
从目标返回近似的状态;
}
public void setApproximatedStanceFromGoal(int ApproximatedStanceFromGoal){
this.approximatedStanceFromGoal=approximatedStanceFromGoal;
}
公共位置getParent(){
返回父母;
}
公共void setParent(位置父项){
this.parent=parent;
}
@凌驾
公共字符串toString(){
返回“Location[Location=“+Arrays.toString(Location)+”,id=“+id
+,description=“+description+”,weight=“+weight
+“,name=“+name+”,exit=“+Arrays.toString(exit)
+"]";
}
}
我不确定自己是否完全理解,但“出口”数组中的名称是否成为子节点?不确定Exit对象上有哪些方法,或者这是否是远程最有效的解决方案,而且很遗憾,我目前无法检查语法
其思想是,该方法获取一个节点,找到子节点,然后在该节点上有效地递归地“遍历”层次结构调用该方法
ArrarList<Location> allNodes
// Create children for the specified node
public void createChildNodes(node n){
// Fetch all available exits
for(Exit e : n.getExit()){
// Iterate through all possible nodes
for(tempNode : allNodes) {
// Check if the 'exit' node matches one of the available nodes
if(tempNode.getName().equals(e.getName()){
// Match found, add it
n.addChildNode(tempNode, tempNode.getDistance));
// Check for children nodes of the temp node
createChildNodes(tempNode);
}
}
}
}
ArrarList所有节点
//为指定节点创建子节点
公共void createChildNodes(节点n){
//获取所有可用的出口
对于(出口e:n.getExit()){
//遍历所有可能的节点
用于(临时节点:所有节点){
//检查“退出”节点是否与其中一个可用节点匹配
if(tempNode.getName().equals)(例如getName()){
//找到匹配项,添加它
n、 addChildNode(tempNode,tempNode.getDistance));
//检查临时节点的子节点
createChildNodes(tempNode);
}
}
}
}
我不确定自己是否完全理解,但“出口”数组中的名称是否成为子节点?不确定Exit对象上有哪些方法,或者这是否是远程最有效的解决方案,而且很遗憾,我目前无法检查语法
其思想是,该方法获取一个节点,找到子节点,然后在该节点上有效地递归地“遍历”层次结构调用该方法
ArrarList<Location> allNodes
// Create children for the specified node
public void createChildNodes(node n){
// Fetch all available exits
for(Exit e : n.getExit()){
// Iterate through all possible nodes
for(tempNode : allNodes) {
// Check if the 'exit' node matches one of the available nodes
if(tempNode.getName().equals(e.getName()){
// Match found, add it
n.addChildNode(tempNode, tempNode.getDistance));
// Check for children nodes of the temp node
createChildNodes(tempNode);
}
}
}
}
ArrarList所有节点
//为指定节点创建子节点
公共void createChildNodes(节点n){
//获取所有可用的出口
new Vertext().set("age",10);
Graph g = new Graph();
g.addVertex("Doctor",new DrVertex("Dr. Smith"));
g.addVertex("Doctor",new DrVertex("Dr. Cooper"));
List<Vertex> doctors = g.get("Doctor");
assertTrue("Dr. Smith",doctors.get(0));
assertTrue("Dr. Cooper",doctors.get(1));
/**
* @author Christian Bongiorno
*/
public class Graph {
private class Vertex {
Map<String,Object> properties;
private Map<String,Edge> edges;
public Graph addVertex(String edgeName, Vertex v) {
Edge e = edges.get(edgeName);
if(e == null) {
e = new Edge(this);
edges.put(edgeName,e);
}
e.addVertex(v);
return Graph.this;
}
public Graph addVertex(Vertex v) {
return addVertex("anonymous",v);
}
}
private static class Edge {
Map<String,Object> properties;
Vertex in;
Collection<Vertex> out;
private Edge(Vertex in) {
this.in = in;
}
private void addVertex(Vertex v) {
out.add(v);
}
}
}
public Location find(Integer locationId) {
Location result = null;
for(Location l : locations) {
// this hashset represents the visited state which only matters for this method call
result = dfs(new HashSet(),l,locationId);
if(result != null)
break;
}
return result;
}
private Location dfs(Set<Location> visitedAlready,Location current, Integer id){
if(current.id == id)
return current;
visitedAlready.add(current); // instead of your boolean
Location result = null;
for(Location l : current.locations) {
result = dfs(visitedAlready,l,id);
if(result != null)
break;
}
return result;
}