检查java.util.List中包含的泛型类

时间:2014-11-22 11:50:27

标签: java generics

我有两个课程如下:

Class#1    
public class Vert<T extends Comparable<? super T>> implements Comparable<Vert<T>> {
    protected T data;

    public Vert(T data) {
        this.data = data;
    }

    @Override
    public int compareTo(Vert<T> other) {
        return data.compareTo(other.data);
    }
}

Class#2
public class Edge implements Comparable<Edge> {
    protected int data;

    public Edge(int data) {
        this.data = data;
    }

    @Override
    public int compareTo(Edge o) {
        return data - o.data;
    }
}

包含这两个类列表的类:

Class#3
public class Comp {
    private static final String CHARSET = "UTF-8";
    private final int NUM_VERTEX;
    private final int NUM_EDGES;
    private List<Vert<Integer>> vertList = new ArrayList<Vert<Integer>>();
    private List<Edge> edgeList = new ArrayList<Edge>();

    public Comp(InputStream in) {
        Scanner scanner = new Scanner(in, CHARSET);
        NUM_VERTEX = scanner.nextInt();
        NUM_EDGES = scanner.nextInt();

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] vertices = line.split(" ");
            int v1 = Integer.parseInt(vertices[0]);
            int v2 = Integer.parseInt(vertices[1]);
            Vert<Integer> vert1 = new Vert<Integer>(v1);
            Vert<Integer> vert2 = new Vert<Integer>(v2);
            Edge e = new Edge(Integer.MIN_VALUE);

            ////////////// error here ////////////////
            if (vertList.contains(vert1)) {

            }
        }
    }

    /////////////// method 1 ///////////////////
    public boolean hasV(Vert<Integer> v) {
        return vertList.contains(v);
    }

    /////////////// method 2 ///////////////////
    public boolean hasEdge(Edge edge) {
        return edgeList.contains(edge);
    }
}

上面装饰的方法2根据包含边缘的edgeList返回值。 但是,上面修饰的方法1给出了未定义contains的编译错误。 我正在学习类型安全和泛型,但无法确定这里的错误,因为List接口在其API中定义了方法contains

[edit]我编辑了源代码来完成示例。抛出的确切错误是: Cannot resolve method contains

1 个答案:

答案 0 :(得分:1)

你这里犯了错误。 v1有两种数据类型,即intVert<Integer>,这在同一范围内是不可能的。

试试这个

类#3

public class Comp {
    private List<Vert<Integer>> vertList = new ArrayList<Vert<Integer>>();
    private List<Edge> edgeList = new ArrayList<Edge>();

    public Comp(InputStream in) {
        Scanner scanner = new Scanner(in, CHARSET);
        NUM_VERTEX = scanner.nextInt();
        NUM_EDGES = scanner.nextInt();

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] vertices = line.split(" ");
            int v1 = Integer.parseInt(vertices[0]);
            int v2 = Integer.parseInt(vertices[1]);
            Vert<Integer> vert1 = new Vert<Integer>(v1); // Change variable name v1 to vert1
            Vert<Integer> vert2 = new Vert<Integer>(v2); // Change variable name v2 to vert2
            Edge e = new Edge(Integer.MIN_VALUE);

            if (vertList.contains(vert1) { // Change variable name v1 to vert1

            }
        }
    }

    /////////////// method 1 ///////////////////
    public boolean hasV(Vert<Integer> v) {
        return vertList.contains(v);
    }

    /////////////// method 2 ///////////////////
    public boolean hasEdge(Edge edge) {
        return edgeList.contains(edge);
    }
}