Can we use [ ] brackets in lists to add elements, like we do in normal arrays?

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

Can we use [ ] brackets in lists to add elements, like we do in normal arrays?

问题

以下是翻译好的部分:

我原以为[]括号只能用在普通数组中,直到我看到下面的回答,

class Solution {
    Set<Integer> seen = new HashSet();
    int MAX_EDGE_VAL = 1000;

    public int[] findRedundantConnection(int[][] edges) {
        ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];
        for (int i = 0; i <= MAX_EDGE_VAL; i++) {
            graph[i] = new ArrayList();
        }

        for (int[] edge: edges) {
            seen.clear();
            if (!graph[edge[0]].isEmpty() && !graph[edge[1]].isEmpty() &&
                    dfs(graph, edge[0], edge[1])) {
                return edge;
            }
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        throw new AssertionError();
    }
    public boolean dfs(ArrayList<Integer>[] graph, int source, int target) {
        if (!seen.contains(source)) {
            seen.add(source);
            if (source == target) return true;
            for (int nei: graph[source]) {
                if (dfs(graph, nei, target)) return true;
            }
        }
        return false;
    }
}

如果你觉得看整个代码有些困惑,以下是我疑惑的部分,

ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];
//通常我看到这一行没有[]括号。
//但是这里的[]括号如何影响列表?如果在声明时添加[]括号,我们可以对列表做哪些操作呢?
            for (int i = 0; i <= MAX_EDGE_VAL; i++) {
                graph[i] = new ArrayList();
                //此外,这里他们正在将列表分配给数组,就像我们在数组中做的那样。
//还有一个疑问是graph列表只接受整数值,但是这里他们为graph列表的每个元素分配了一个新列表,
//而不是像在声明时指定的整数。
            }

我的疑惑在上面的代码注释中包含。

英文:

I thought [] brackets can only be used in normal arrays until I came across this answer below,

class Solution {
    Set&lt;Integer&gt; seen = new HashSet();
    int MAX_EDGE_VAL = 1000;

    public int[] findRedundantConnection(int[][] edges) {
        ArrayList&lt;Integer&gt;[] graph = new ArrayList[MAX_EDGE_VAL + 1];  //Normally I&#39;ve seen This line without [] brackets. But How [] brackets here impacts the list. What are the things we can do to a list if [] brackets are added at declaraton.
        for (int i = 0; i &lt;= MAX_EDGE_VAL; i++) {
            graph[i] = new ArrayList(); //Also Here they are assigning a list like we do in an array. Also my another doubt is graph list accepts only Integer values but here they are assigning new list to each element of graph list instead of integer which they have specified during declaration
        }

        for (int[] edge: edges) {
            seen.clear();
            if (!graph[edge[0]].isEmpty() &amp;&amp; !graph[edge[1]].isEmpty() &amp;&amp;
                    dfs(graph, edge[0], edge[1])) {
                return edge;
            }
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        throw new AssertionError();
    }
    public boolean dfs(ArrayList&lt;Integer&gt;[] graph, int source, int target) {
        if (!seen.contains(source)) {
            seen.add(source);
            if (source == target) return true;
            for (int nei: graph[source]) {
                if (dfs(graph, nei, target)) return true;
            }
        }
        return false;
    }
}

If you find confusing looking at the full code, Here is the part where my doubt arises,

ArrayList&lt;Integer&gt;[] graph = new ArrayList[MAX_EDGE_VAL + 1];  //Normally I&#39;ve seen This line without [] brackets. 
//But How [] brackets here impacts the list. What are the things we can do to a list if [] brackets are added at declaraton.
            for (int i = 0; i &lt;= MAX_EDGE_VAL; i++) {
                graph[i] = new ArrayList(); //Also Here they are assigning a list like we do in an array.
 //Also my another doubt is graph list accepts only Integer values but here they are assigning new list to each element of graph list instead of integer which they have specified during declaration
            }

My Doubt is included in the above code as comments.

答案1

得分: 2

在列表中我们可以像在普通数组中那样使用 [ ] 方括号来添加元素吗?

不可以。(显然在 Kotlin 中可以,但 Java 中不行。)

ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];

这是声明了一个列表的数组。

graph[i] = new ArrayList();

这是将一个列表添加到了列表的数组中。这是标准的数组语法,在数组上操作

英文:

> Can we use [ ] brackets in lists to add elements, like we do in normal arrays?

No. (Apparently you can with Kotlin ... but not Java.)

> ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];

This is declaring an array of lists.

> graph[i] = new ArrayList();

This is adding a list to the array of lists. This is standard array syntax operating on an array.

huangapple
  • 本文由 发表于 2020年8月30日 12:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654082.html
匿名

发表评论

匿名网友

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

确定