eclipse - Java generics for defining a graph -
(full disclosure, homework.)
so have been given vertex class build graph , perform standard graph algorithms. here's part of vertex class given (this classes cannot changed, i'll try , make clear can , cannot modified assignment):
public static final class vertex<v> { private final string vertex; private final v vertexdata; protected vertex(string v, v d) { vertex = v; vertexdata = d; } ... //some methods } the vertex class embedded defined in interface igraph<v,e> has method:
public void addvertex( string vertexname, v vertexdata ) throws duplicatevertexexception { in implementation of interface, implement method second parameter object (not precisely sure why, error if don't) have following:
//this code, , can changed public class graph<v, e> implements igraph{ private boolean directed = false; private list<vertex<v>> vertices = new arraylist<vertex<v>>(); private list<edge<e>> edges = new arraylist<edge<e>>(); public void addvertex(string vertexname, object vertexdata) throws duplicatevertexexception { vertex<v> vnew = new vertex(vertexname, vertexdata); //this problem for(vertex<v> x: vertices) { if(vertexname.equals(x.getvertexname())) { throw new duplicatevertexexception(); } } vertices.add(vnew); } ... (other methods) } basically need java generics here. if try put in
vertex<v> vnew = new vertex<v>(vertexname, vertexdata); i eclipse telling me the constructor igraph.vertex<v>(string, object) undefined. if put in
vertex<object> vnew = new vertex<object>(vertexname, vertexdata); the type mismatch when want add vertices list. thing eclipse doesn't give me error when use raw type
vertex<object> vnew = new vertex(vertexname, vertexdata); but i've been encouraged not use raw types, i'd avoid this. should here? line problem or there else should change, typing on list of vertices?
basically i'm trying figure out how generic types work in case, because seems generic typing of class wants me make vertex<v>, method signature i'm required take argument of type object.
any hugely appreciated, other resources or explanations regarding generic types appreciated, i've read few including official oracle doumentation, i'm still confused on parts of it.
Comments
Post a Comment