可以制作一个对象的ArrayList吗?

huangapple go评论71阅读模式
英文:

Java: Can I make an ArrayList of objects?

问题

以下是您提供的内容的翻译部分:

我有以下要做的事情:可以制作一个对象的ArrayList吗?

我已经创建了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:可以制作一个对象的ArrayList吗?

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 = &quot;default name&quot;;
}
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&lt;ArrayList&lt;Node&gt;&gt; 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&#39;t understand why these give opposite errors like java do you want an int or a node????
static void addEdge(ArrayList&lt;ArrayList&lt;Node&gt;&gt; gr, Edge edge) {   &lt;---- Initially made list of Node type but I&#39;m noob and it doesn&#39;t work
gr.get(edge.getNodeA().getNr()).add(edge.getNodeB().getNr());
gr.get(edge.getNodeB()).add(edge.getNodeA());
}
*/
static void addEdge(ArrayList&lt;ArrayList&lt;Integer&gt;&gt; gr, int s, int d) {
gr.get(s).add(d);
gr.get(d).add(s);
}
/*next todo
static void delEdge(ArrayList&lt;ArrayList&lt;Integer&gt;&gt; gr,
int s, int d)
*/
public static void makeGraph(){
int NR=4; //I&#39;ve tried to make it dynamic but I&#39;ve no idea how (like, adjust it&#39;s size when adding edges (will do)
ArrayList&lt;ArrayList&lt;Integer&gt;&gt; gr = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;(NR);
for (int i = 0; i &lt; NR; i++)
gr.add(new ArrayList&lt;Integer&gt;());
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&lt;ArrayList&lt;Integer&gt;&gt; gr) {
for (int i = 0; i &lt; gr.size(); i++) {
System.out.println(&quot;\nNode &quot; + i + &quot;:&quot;);
for (int j = 0; j &lt; gr.get(i).size(); j++) {
System.out.print(&quot; -&gt; &quot; + 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&lt;Node&gt; nodes;
Set&lt;Edge&gt; edges;
public GraphModel() {
this.nodes = new HashSet&lt;&gt;();
this.edges = new HashSet&lt;&gt;();
}
// 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&lt;Edge&gt; edgesToRemove = new ArrayList&lt;&gt;();
// 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, &quot;A&quot;);
Node B = new Node(1, 1, 1, 1, &quot;B&quot;);
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 &quot;GraphModel{&quot; +
&quot;nodes=&quot; + nodes +
&quot;, edges=&quot; + edges +
&#39;}&#39;;
}
}

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 = &quot;default name&quot;;
}
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 &quot;Node{&quot; +
&quot;name=&#39;&quot; + name + &#39;\&#39;&#39; +
&quot;, x=&quot; + rectangle.x +
&quot;, y=&quot; + rectangle.y +
&quot;, width=&quot; + rectangle.width +
&quot;, height=&quot; + rectangle.height +
&#39;}&#39;;
}
}

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 &quot;Edge{&quot; +
&quot;nodeA=&quot; + nodeA +
&quot;, nodeB=&quot; + nodeB +
&#39;}&#39;;
}
}

huangapple
  • 本文由 发表于 2020年6月5日 20:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/62215465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定