为了减少代码重复,我替换了这段代码
ArrayList<Vertex> allVertice = new ArrayList<>(hypergraph.getVertices());
System.out.println(allVertice.size());
//hypergraph.getVerticebyType();
ArrayList<ImageVertex> allImVetice = new ArrayList<>();
ArrayList<TagVertex> allTagVetice = new ArrayList<>();
ArrayList<LocationVertex> allLocVetice = new ArrayList<>();
for (Vertex vertex : allVertice) {
if (vertex instanceof ImageVertex)
allImVetice.add((ImageVertex)vertex);
else if (vertex instanceof TagVertex)
allTagVetice.add((TagVertex)vertex);
else if (vertex instanceof LocationVertex)
allLocVetice.add((LocationVertex)vertex);
}
通过这种方法
public <T extends Vertex<?> > ArrayList<T> getVerticebyType( ) {
ArrayList<T> array = new ArrayList<T>();
for (Vertex<?> vertex : this.getAllVertice()) {
if (vertex instanceof Vertex<?>)
array.add((T) vertex);
}
return array;
}
但是当我测试这个指令时
ArrayList<ImageVertex>=hyperGraph.getVerticebyType();
它返回所有顶点,不管它们的类型!
有什么问题以及如何解决?