我在这里发现了许多相关的帖子,但无法得到我的答案。为什么这个运行时错误?Array ArrayList的Java空指针异常
static List<Integer>[] adj = (List<Integer>[]) new ArrayList[1000];
public static void main(String[] args) {
int edge, u, v, source;
Scanner input = new Scanner(System.in);
edge = input.nextInt();
for (int i = 0; i < edge; i++) {
u = input.nextInt();
v = input.nextInt();
adj[v].add(u); // Null pointer Exception
adj[u].add(v); // Null pointer Exception
}
仅仅因为您创建了数组,它并不意味着每个条目(每个'ArrayList')都被创建。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – madth3