0

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

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
4

2 回答 2

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

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

private List<Connection> outboundConnections = new ArrayList<Connection>();
于 2012-12-06T23:31:49.697 回答
2

显然你需要添加

import java.sql.Connection;
于 2012-12-06T23:34:38.273 回答