1

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

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];  //Normally I'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 <= 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() && !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;
    }
}

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

ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];  //Normally I'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 <= 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.

ALLAN
  • 71
  • 6

1 Answers1

2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • @Stephan_C, So, Is ArrayList[] graph , same as declaring ArrayList> graph ? – ALLAN Aug 30 '20 at 05:25
  • No. Absolutely not. `ArrayList[]` declares an array of lists. `ArrayList>` declares a list of lists. Arrays and lists are not equivalent. (Also, I don't see how you have made the "logical jump" from subscripting an array of lists to array of list being equivalent to list of list.) – Stephen C Aug 30 '20 at 05:27
  • Think i got it. – ALLAN Aug 30 '20 at 05:30
  • So the first one is just basically an array ? – ALLAN Aug 30 '20 at 05:30
  • An array of lists is an array ... of lists. (Like, a bag of fruit is a bag ... of fruit.) – Stephen C Aug 30 '20 at 05:31
  • Yep , like a normal array which just basically contains elements . Here This normal array just contains lists instead of elements.. right? – ALLAN Aug 30 '20 at 05:33
  • Correct. The word "of" is being used in the normal English sense here. The lists >>are<< the elements of the array. – Stephen C Aug 30 '20 at 05:34