Graph test = new Graph(testVertices, testEdges); I'm a bit confused as to why I am getting the following results:
List<Vertex> temp = test.getVertices();
System.out.println(testVertices.size()); //size is 10 as expected
System.out.println(temp.size()); //size is now 0... but should be 10
This is my getVertices() method below
public List<Vertex> getVertices(){
List<Vertex> theVertices = new ArrayList<Vertex>();
for(int i=0;i<V.size();i++){
System.out.println("happen");//never reached...
theVertices.add(V.get(i));
}
return theVertices;
}
I should be getting 10 when I call temp.size(), but I don't understand why I am getting 0. Could someone explain?
Thanks.
Editted
import java.util.*; public class Graph {
private ArrayList<Vertex> V;
private ArrayList<Edge> E;
//constructor
public Graph(Collection<Vertex> v, Collection<Edge> e){
V=new ArrayList<Vertex>();
E=new ArrayList<Edge>();
}
public List<Vertex> getVertices(){
List<Vertex> theVertices = new ArrayList<Vertex>();
for(int i=0;i<V.size();i++){
System.out.println("happen");
theVertices.add(V.get(i));
}
return theVertices;
}
public static void main(String[] args){
Vertex A = new Vertex("A");
Vertex B = new Vertex("B");
Vertex C = new Vertex("C");
Vertex D = new Vertex("D");
Vertex E = new Vertex("E");
Vertex F = new Vertex("F");
Vertex G = new Vertex("G");
Vertex H = new Vertex("H");
Vertex I = new Vertex("I");
Vertex J = new Vertex("J");
List<Vertex> testVertices = new ArrayList<Vertex>();
testVertices.add(A);
testVertices.add(B);
testVertices.add(C);
testVertices.add(D);
testVertices.add(E);
testVertices.add(F);
testVertices.add(G);
testVertices.add(H);
testVertices.add(I);
testVertices.add(J);
Graph test = new Graph(testVertices, testEdges);
List<Vertex> temp = test.getVertices();
System.out.println(testVertices.size());
System.out.println(temp.size()); //size is now 0...
for(int i=0;i<temp.size();i++){
System.out.println(temp.get(i).getLabel());
}
V? If theVvariable is initialised to default,V.size()will be 0, and the loop won't execute.Graphclass then? With this fragment, it's hard to say what could be the issue, although I suspect it's what I mentioned in my first comment.