英文:
Java: Can I make an ArrayList of objects?
问题
以下是您提供的内容的翻译部分:
我已经创建了Node和Edge类(希望我理解正确),但在GraphModel类上遇到了困难。以下是我目前的代码:
Node.java(忽略getNr)
import java.awt.Rectangle;
public class Node {
private String name;
private Rectangle rectangle;
public Node(int x, int y, int width, int height, String name) {
this.rectangle = new Rectangle(x, y, width, height);
this.name = name;
}
public Node() {
this.rectangle = new Rectangle(0, 0, 0, 0); //稍后会更改参数
this.name = "默认名称";
}
public int getNr() {
return nr;
}
public String getName() {
return name;
}
}
Edge.java:
public class Edge {
private Node nodeA;
private Node nodeB;
public Edge(Node nodeA, Node nodeB) {
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public Node getNodeA() {
return nodeA;
}
public Node getNodeB() {
return nodeB;
}
}
现在,我希望它们是正确的,但如果您注意到有什么不对的地方,请告诉我。
现在谈谈GraphModel,我使用了一个根据给定节点数创建图形的代码(这不太好,因为我想在添加/删除节点时增加/减少N(节点数,在代码中可以看到)。此外,节点由整数表示,我想知道如何将它们表示为我创建的实际节点类(因为以后我将不得不为此创建GUI)。我尝试过使用ArrayList<ArrayList<Node>>
,但不知道接下来该怎么做。以下是代码:
GraphModel.java:
import java.util.*;
public class GraphModel {
private int numNode;
private Node node;
static void addEdge(ArrayList<ArrayList<Integer>> gr, int s, int d) {
gr.get(s).add(d);
gr.get(d).add(s);
}
public static void makeGraph() {
int NR = 4; //我尝试过使它动态变化,但不知道如何(调整其大小以适应添加边缘时)
ArrayList<ArrayList<Integer>> gr = new ArrayList<ArrayList<Integer>>(NR);
for (int i = 0; i < NR; i++)
gr.add(new ArrayList<Integer>());
GraphModel.addEdge(gr, 0, 1);
GraphModel.addEdge(gr, 0, 2);
GraphModel.addEdge(gr, 0, 3);
GraphModel.addEdge(gr, 1, 2);
GraphModel.addEdge(gr, 1, 3);
printGraph(gr);
}
static void printGraph(ArrayList<ArrayList<Integer>> gr) {
for (int i = 0; i < gr.size(); i++) {
System.out.println("\n节点 " + i + ":");
for (int j = 0; j < gr.get(i).size(); j++) {
System.out.print(" -> " + gr.get(i).get(j));
}
System.out.println();
}
}
}
希望这是有意义的,如果不是,请随时问我任何问题。还请记住,我刚刚开始学习面向对象编程,大约一周前开始。非常感谢您的时间!
英文:
I have the following thing to do:
I've made the Node and Edge class (I hope I got them right) and I'm stuck on the GraphModel class. Here is my code so far:
Node.java (ignore the getNr)
import java.awt.Rectangle;
public class Node {
private String name;
private Rectangle rectangle;
public Node(int x, int y, int width, int height, String name) {
this.rectangle =new Rectangle(x, y, width, height);
this.name = name;
}
public Node() {
this.rectangle = new Rectangle (0,0,0,0); //gonna change the parameters later
this.name = "default name";
}
public int getNr() {
return nr;
}
public String getName() {
return name;
}
}
Edge.java:
public class Edge {
private Node nodeA;
private Node nodeB;
public Edge(Node nodeA, Node nodeB) {
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public Node getNodeA() {
return nodeA;
}
public Node getNodeB() {
return nodeB;
}
}
Now, I hope they are correct but if you notice there is something not right please let me know.
Now about the GraphModel, I used a code that makes a graph with a given number of nodes (which is not good because I want to increase/decrease the N (number of nodes, you will see in the code) when I add/remove a node) . Also, the nodes are represented by integers, I wonder how can I make them be represented by the actual node class that I've created (because I will later have to make a GUI for that). I've tried to use ArrayList<ArrayList<Node>>
but I have no idea what should I do next. Here is the code
GraphModel.java:
import java.util.*;
public class GraphModel {
private int numNode;
private Node node;
/* Add edge for undirected graph (I don't understand why these give opposite errors like java do you want an int or a node????
static void addEdge(ArrayList<ArrayList<Node>> gr, Edge edge) { <---- Initially made list of Node type but I'm noob and it doesn't work
gr.get(edge.getNodeA().getNr()).add(edge.getNodeB().getNr());
gr.get(edge.getNodeB()).add(edge.getNodeA());
}
*/
static void addEdge(ArrayList<ArrayList<Integer>> gr, int s, int d) {
gr.get(s).add(d);
gr.get(d).add(s);
}
/*next todo
static void delEdge(ArrayList<ArrayList<Integer>> gr,
int s, int d)
*/
public static void makeGraph(){
int NR=4; //I've tried to make it dynamic but I've no idea how (like, adjust it's size when adding edges (will do)
ArrayList<ArrayList<Integer>> gr = new ArrayList<ArrayList<Integer>>(NR);
for (int i = 0; i < NR; i++)
gr.add(new ArrayList<Integer>());
GraphModel.addEdge(gr,0,1);
GraphModel.addEdge(gr,0,2);
GraphModel.addEdge(gr,0,3);
GraphModel.addEdge(gr,1,2);
GraphModel.addEdge(gr,1,3);
printGraph(gr);
}
static void printGraph(ArrayList<ArrayList<Integer>> gr) {
for (int i = 0; i < gr.size(); i++) {
System.out.println("\nNode " + i + ":");
for (int j = 0; j < gr.get(i).size(); j++) {
System.out.print(" -> " + gr.get(i).get(j));
}
System.out.println();
}
}
}
Hope that makes sense, if not feel free to ask me anything. Also keep in mind that I am a beginner in Object oriented programming, started it like 1 week ago. Thank you very much for your time!
答案1
得分: 1
以下是翻译好的代码部分:
GraphModel 类
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class GraphModel {
Set<Node> nodes;
Set<Edge> edges;
public GraphModel() {
this.nodes = new HashSet<>();
this.edges = new HashSet<>();
}
// 添加节点
public void addNode(Node node) {
nodes.add(node);
}
// 添加边
public void addEdge(Edge edge) {
edges.add(edge);
}
// 移除节点
public void removeNode(Node node) {
nodes.remove(node);
// 移除与该节点相连的所有边
List<Edge> edgesToRemove = new ArrayList<>();
// 获取与该节点相连的所有边
for (Edge edge : edges) {
if (edge.getNodeA().equals(node) || edge.getNodeB().equals(node)) {
edgesToRemove.add(edge);
}
}
// 移除边
edges.removeAll(edgesToRemove);
}
// 移除一条边
public void removeEdge(Edge edge) {
edges.remove(edge);
}
public static void main(String[] args) {
GraphModel graph = new GraphModel();
Node A = new Node(0, 0, 1, 1, "A");
Node B = new Node(1, 1, 1, 1, "B");
graph.addNode(A);
graph.addNode(B);
Edge edge = new Edge(A, B);
graph.addEdge(edge);
System.out.println(graph);
graph.removeNode(A);
System.out.println(graph);
}
@Override
public String toString() {
return "GraphModel{" +
"nodes=" + nodes +
", edges=" + edges +
'}';
}
}
Node 类
import java.awt.*;
public class Node {
private String name;
private Rectangle rectangle;
public Node(int x, int y, int width, int height, String name) {
this.rectangle = new Rectangle(x, y, width, height);
this.name = name;
}
public Node() {
this.rectangle = new Rectangle(0, 0, 0, 0); //稍后将更改参数
this.name = "默认名称";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
@Override
public boolean equals(Object o) {
Node node = (Node) o;
return this.name.equals(node.getName());
}
@Override
public String toString() {
return "Node{" +
"name='" + name + '\'' +
", x=" + rectangle.x +
", y=" + rectangle.y +
", width=" + rectangle.width +
", height=" + rectangle.height +
'}';
}
}
Edge 类
public class Edge {
private Node nodeA;
private Node nodeB;
public Edge(Node nodeA, Node nodeB) {
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public Node getNodeA() {
return nodeA;
}
public Node getNodeB() {
return nodeB;
}
@Override
public String toString() {
return "Edge{" +
"nodeA=" + nodeA +
", nodeB=" + nodeB +
'}';
}
}
英文:
You can do this
For GraphModel class
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class GraphModel {
Set<Node> nodes;
Set<Edge> edges;
public GraphModel() {
this.nodes = new HashSet<>();
this.edges = new HashSet<>();
}
// add node
public void addNode(Node node) {
nodes.add(node);
}
// add edge
public void addEdge(Edge edge) {
edges.add(edge);
}
// remove node
public void removeNode(Node node) {
nodes.remove(node);
// remove all edges who are connected with this node
List<Edge> edgesToRemove = new ArrayList<>();
// get all edges who are connected to this node
for(Edge edge: edges){
if(edge.getNodeA().equals(node) || edge.getNodeB().equals(node)){
edgesToRemove.add(edge);
}
}
// remove edges
edges.removeAll(edgesToRemove);
}
// remove an edge
public void removeEdge(Edge edge) {
edges.remove(edge);
}
public static void main(String[] args) {
GraphModel graph = new GraphModel();
Node A = new Node(0, 0, 1, 1, "A");
Node B = new Node(1, 1, 1, 1, "B");
graph.addNode(A);
graph.addNode(B);
Edge edge = new Edge(A, B);
graph.addEdge(edge);
System.out.println(graph);
graph.removeNode(A);
System.out.println(graph);
}
@Override
public String toString() {
return "GraphModel{" +
"nodes=" + nodes +
", edges=" + edges +
'}';
}
}
For Node class
import java.awt.*;
public class Node {
private String name;
private Rectangle rectangle;
public Node(int x, int y, int width, int height, String name) {
this.rectangle = new Rectangle(x, y, width, height);
this.name = name;
}
public Node() {
this.rectangle = new Rectangle(0, 0, 0, 0); //gonna change the parameters later
this.name = "default name";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
@Override
public boolean equals(Object o) {
Node node = (Node) o;
return this.name.equals(node.getName());
}
@Override
public String toString() {
return "Node{" +
"name='" + name + '\'' +
", x=" + rectangle.x +
", y=" + rectangle.y +
", width=" + rectangle.width +
", height=" + rectangle.height +
'}';
}
}
and for Edge class
public class Edge {
private Node nodeA;
private Node nodeB;
public Edge(Node nodeA, Node nodeB) {
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public Node getNodeA() {
return nodeA;
}
public Node getNodeB() {
return nodeB;
}
@Override
public String toString() {
return "Edge{" +
"nodeA=" + nodeA +
", nodeB=" + nodeB +
'}';
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论