编译列表时出错

时间:2012-12-06 23:30:28

标签: java arraylist

我正在尝试一些代码,但我无法编译它。有什么错误吗?我已附上以下代码。

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;

public class Node
{
  private int nodeId;
  private List<Connection> outboundConnections = new ArrayList<>();

  public Node(int nodeId)
  {
    this.nodeId = nodeId;
  }

  public void addConnection(Connection connection)
  {
    this.outboundConnections.add(connection);
  }
}

当我编译时,我遇到了这种错误:

Node.java:9: error: cannot find symbol

  private List<Connection> outboundConnections = new ArrayList<>();

               ^
  symbol:   class Connection

  location: class Node

Node.java:16: error: cannot find symbol

  public void addConnection(Connection connection)
                            ^
  symbol:   class Connection

  location: class Node

Node.java:9: error: unexpected type

  private List<Connection> outboundConnections = new ArrayList<>();

                                                              ^
  required: class

  found:    <E>ArrayList<E>

  where E is a type-variable:

    E extends Object declared in class ArrayList

3 errors

2 个答案:

答案 0 :(得分:6)

private List<Connection> outboundConnections = new ArrayList<>();

仅在Java 7+上有效。如果您运行Java 6或更早版本,则需要使用:

private List<Connection> outboundConnections = new ArrayList<Connection>();

答案 1 :(得分:2)

显然你需要添加

import java.sql.Connection;