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<Edge>[] graph = new ArrayList[7];
//Doubt regarding this line ,as why is it essential?
graph[0] = new ArrayList<Edge>();
//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();
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?