创建邻接表图

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

Creating adjacency List Graph

问题

我正试图通过创建类型为“Edge”的数组列表数组(包括源、目标、权重)来为图创建邻接表,如下面的代码所示:

public class Main {
    static class Edge {
        int s;
        int d;
        int wt;

        Edge(int src, int des, int weight) {
            this.s = src;
            this.d = des;
            this.wt = weight;
        }
    }

    public static void main(String[] args) throws Exception {
        // 创建类型为 Edge 的数组列表数组
        ArrayList<Edge>[] graph = new ArrayList[7];

        // 对于这一行存在疑问,为什么是必要的呢?
        graph[0] = new ArrayList<Edge>();

        // 创建 Edge 对象
        Edge e = new Edge(0, 1, 10);

        // 将其添加到数组列表中
        graph[0].add(e);
    }
}

由于我已经创建了类型为“Edge”的数组列表数组,我认为我可以直接像 graph[0].add(e) 这样添加到数组列表中,而不需要编写 graph[0] = new ArrayList<Edge>(); 这一行代码。

但是如果没有这行代码,就不能正常工作。为什么我需要提供上述语句?既然我的数组是数组列表,我不能直接添加元素吗?

英文:

I am trying to create adjacency list for graph by creating array of arraylist of type edge(include source,dest,weight) as in code below :

public class Main {
	
static class Edge{
	
	int s;
	int d;
	int wt;
	
	Edge(int src,int des,int weight)
	{
		this.s = src;
		this.d = des;
		this.wt = weight;
		
	}
}
	
	public static void main(String[] args) throws Exception
	{
		//creating array of arraylist of type edge

		ArrayList&lt;Edge&gt;[] graph = new ArrayList[7];
		
		//Doubt regarding this line ,as why is it essential?
		graph[0] = new ArrayList&lt;Edge&gt;();
		
        //creating edge object 
		Edge e = new Edge(0,1,10);

      //adding it into arraylist
		graph[0].add(e);
		
		
		
	}

Since I have created array of arraylist of type edge , I think I can directly add into arraylist like graph[0].add(e) without writing
graph[0] = new ArrayList<Edge>();

but it isn't working without it. Why I need to give above statement when my array is of arraylist so can't I add the elements directly?

答案1

得分: 0

这段代码声明了一个由 7 个 ArrayList 组成的数组 graph,其元素最初(与所有数组一样)被设置为它们的默认值 - 对象为 null,整数为 0,布尔值为 false

ArrayList<Edge>[] graph = new ArrayList[7];

你可以通过添加以下代码来进行测试,或者使用调试器逐步执行:

System.err.println(graph[0]); // 输出 "null"

你的 new ArrayList[7] 仅为一个(ArrayList)对象数组保留了空间,但没有为你想要添加的 7 个新 ArrayList 保留空间。这允许你添加 ArrayList 的子类到数组中,或者将某些槽位保留为空值,或者将相同的 ArrayList 添加到多个槽位。所有这些选项在某些情况下都可能有用,因此编译器和语言不会创建空的新元素,除非你明确要求这样做。我建议使用循环来完成:

ArrayList<Edge>[] graph = new ArrayList[7];
for (int i=0; i<graph.length; i++) {
    graph[i] = new ArrayList<Edge>();
}
英文:

This code declares graph to be an array of 7 ArrayList, with its elements initially (as in all arrays) set to their default values - null for objects, 0 for integers, false for booleans:

    ArrayList&lt;Edge&gt;[] graph = new ArrayList[7];

You can test it by adding this line below, or by stepping through with a debugger:

    System.err.println(graph[0]); // prints &quot;null&quot;

Your new ArrayList[7] only reserved space for an array of (ArrayList) objects, but did not reserve space for the 7 new ArrayLists you want to add. This allows you, for instance, to add subclasses of ArrayList to your array, or leave some slots with a null value, or add the same ArrayList to several slots. All of these options can be useful in some cases, so the compiler and language do not create empty new elements unless you explicitly them to do so. I would recommend using a loop:

    ArrayList&lt;Edge&gt;[] graph = new ArrayList&lt;&gt;[7];
    for (int i=0; i&lt;graph.length; i++) {
        graph[i] = new ArrayList&lt;Edge&gt;();
    }

huangapple
  • 本文由 发表于 2020年9月21日 16:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63989067.html
匿名

发表评论

匿名网友

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

确定