What is ArrayList<Integer>[] or StringBuilder[]? What is this square bracket's role?
Explanation
It was hard to google it because google search doesnt work well with symbols. However, I found out that
ArrayList<Integer>[] G = new ArrayList[n];
StringBuilder[] st= new StringBuilder[numRows];
is actually just making an array of ArrayLists or StringBuilders.
The main difference between ArrayList
It’s important to note that this notation is considered as
non-type-safe, hence it is not recommended to use them.
Instead, you can use List<ArrayList
To convert the type with casting,
List<Integer>[] graph = (ArrayList<Integer>[]) new ArrayList[n];
When you are building arraylist in that array,
List<Integer>[] graph = (ArrayList<Integer>[]) new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
//not graph[i].add(new ArrayList<>())
}