public class MyGraph implements Graph<Integer,Double>{ private ArrayList<MyVerte
ID: 3778738 • Letter: P
Question
public class MyGraph implements Graph<Integer,Double>{
private ArrayList<MyVertex> vertices = new ArrayList<MyVertex>(); // list of vertices idexed by the vertex index
public class MyVertex implements Vertex<Integer>{
private int index; // index of vertex in adjacency matrix valie in (0..numVertices-1)
private String name; // label/name of vertex
private int degree; // degree of vertex
public MyVertex(int index, String name) {
this.index=index;
this.name=name;
this.degree=0;
}
public String getName() {
return name;
}
@Override
public Integer getElement() {
return index;
}
}
for (int i = 0; i < end1.length; i++) {
String nname = names[cnt];
vertices.add(new MyVertex(cnt,nname));
cnt ++;
}
//***********
// vertices - index 1 replace degree with value = 2
vertices.set(1, MyVertex(1,"Test",2)); // giving an error
}
}
why am I getting error for this program?Is the syntax correct?
Explanation / Answer
vertices.set(1, MyVertex(1,"Test",2));
The above line is causing error because it is a syntactic error.
Actually the function definition for set() method is not given but according to Java rules :
If you want to send a class object we should create and send it using new operator like shown below :
vertices.set(1, new MyVertex(1,"Test",2));
There may be another error also for object creation like shown above because there is no constructor of MyVertex that takes 3 parameters.the constructor definition is only available for 2 parameters (I.e one integer and one string object)
So the line can be modified further like :
vertices.set(1, new MyVertex(1,"Test"));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.