使用 ArrayLists 在 Java 中创建图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21107090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating graphs in Java with ArrayLists
提问by Liondancer
I'm new to Java and I wanted to practice by creating a graph. I am having trouble creating the graph because I'm not sure how to do so correctly. I am lacking the logic and being new in Java makes things quite difficult. I was wondering if anyone could help me or guide me in the right path! Thanks!
我是 Java 新手,我想通过创建图表来练习。我在创建图表时遇到问题,因为我不确定如何正确执行此操作。我缺乏逻辑,而且是 Java 新手让事情变得非常困难。我想知道是否有人可以帮助我或引导我走正确的道路!谢谢!
Basically I am trying to create something like this as a graph. A node will contain an ArrayList
for it's neighbors.
基本上我试图创建这样的东西作为图表。一个节点将包含ArrayList
它的邻居。
A,B,C,D
A = 0,1,3,0
B = 1,0,0,2
C = 1,0,0,3 //shorter path to A (cycle)
D = 0,0,3,0
The nodes are connected to each other with or without weights (if I change numbers to 1)
节点在有或没有权重的情况下相互连接(如果我将数字更改为 1)
Here is what I have so far (it is incomplete):
这是我到目前为止所拥有的(不完整):
public class GraphNode {
boolean visited;
public GraphNode() {
List<GraphNode> node = new ArrayList<GraphNode>(); // store neighbors
this.visited = false;
}
public void addNeighbor(GraphNode root, GraphNode target, int distance) {
root.add(target); // cannot use "add" to ArrayList
}
}
采纳答案by npinti
To be able to access the ArrayList
from accross methods within the sameclass, you would need to promote that local variable to a global variable (field), like below:
为了能够访问同一类中的ArrayList
from accross 方法,您需要将该局部变量提升为全局变量(字段),如下所示:
public class GraphNode {
/*Global Variables*/
boolean visited;
List<GraphNode> nodeNeighbours;
/*Global Variables*/
public GraphNode() {
this.nodeNeighbours = new ArrayList<GraphNode>(); // store neighbors
this.visited = false;
}
public void addNeighbor(GraphNode target) {
this.nodeNeighbours.add(target); //This will add target to the neighbours of this given node.
}
}
EDIT:
Shortest path algorithms (as far as memory serves) have a fixed starting point, meaning that the root will always be the same. The same can be said to their weight, which usually does not change.
编辑:
最短路径算法(就内存而言)有一个固定的起点,这意味着根将始终相同。他们的体重也是如此,通常不会改变。
However, as different paths are explored, the distance to the root node is most likely to change. With that in mind, you could re write your class as follows:
然而,随着探索不同的路径,到根节点的距离最有可能发生变化。考虑到这一点,您可以按如下方式重新编写类:
public class GraphNode {
/*Global Variables*/
protected double weight;
protected double distanceFromRoot;
protected List<GraphNode> neighbours;
protected boolean visited;
/*Global Variables*/
public GraphNode(double weight, double distanceFromRoot) {
this.weight = weight;
this.distanceFromRoot = distanceFromRoot;
this.neighbours = new ArrayList<GraphNode>();
this.visited = false;
}
public void setDistanceFromRoot(double distanceFromRoot) {
this.distanceFromRoot = distanceFromRoot;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public double getWeight() {
return this.weight;
}
public double getDistanceFromRoot() {
return this.distanceFromRoot;
}
public List<GraphNode> getNeighbours() {
return this.neighbours;
}
public void addNeighbour(GraphNode neighbour) {
this.neighbours.add(neighbour)
}
}
This might be a little bit more extensive than what you started with. The main change in your code is that this code promoted encapsulation. Encapsulation is one of the core fundamentals of OOP in which you essentially deny direct access to your global variables. The access is offered through appropriate set
and get
method which you can use to define how and when does someone external to your program modifies the internal state of your program.
这可能比您开始时更广泛一些。您的代码中的主要更改是此代码提升了封装性。封装是 OOP 的核心基础之一,您本质上拒绝直接访问全局变量。该访问是通过适当的提供set
和get
您可以使用它来定义如何以及何时方法确实有人外部程序修改程序的内部状态。