This seems like basic question, but it has really confused me. I ma trying to represent the adjacency list of a graph. I have two questions :
public class Graph
{
private final int V;
private List<Integer>[] adj;
public Graph(int V)
{
this.V = V;
this.adj = (List<Integer>[]) new LinkedList[V]; // this works
}
}
Question 1: when I do the below, it gives an error saying
Array type expected; found: 'java.util.LinkedList<java.lang.Integer>'
this.adj = (List<Integer>[]) new LinkedList<Integer>()[V];
I am creating a List of Integer arrays, right ?
Question 2: when I do this, it again gives an error saying generic array creation:
this.adj = (List<Integer>[]) new LinkedList<Integer>[V];
What is the problem with the last two approaches ? i think the first one is more correct.