它说我必须初始化adj
但它不起作用。我知道有些事情是错误的。我也尝试在ArrayList中使用ArrayList,但这也不起作用。你觉得怎么了?
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AdjacencyList {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter nodes below like (u,v):");
//Create List
ArrayList<Integer>[] adj;
for (int i=0; i<2; i++) {
String nodes = in.nextLine();
String[] data = nodes.split(",");
String u = data[0];
String v = data[1];
int inNode = Integer.parseInt(u);
int outNode =Integer.parseInt(v);
adj[inNode] = new ArrayList<Integer>();
adj[inNode].add(outNode);
System.out.println("Added to list");
}
//print nodes and then their corresponding list
System.out.println(adj[1]);
}
}
答案 0 :(得分:1)
假设你真的想要ArrayList
的数组,你应该像这样初始化它:
ArrayList<Integer>[] adj = (ArrayList<Integer>[]) new ArrayList[SIZE];