1

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());
    }
5
  • What's V? If the V variable is initialised to default, V.size() will be 0, and the loop won't execute. Commented Apr 13, 2014 at 5:54
  • @shree.pat18: V is one of my field variables, private ArrayList<Vertex> V; Commented Apr 13, 2014 at 5:55
  • And where do you declare/define it? Commented Apr 13, 2014 at 5:55
  • @shree.pat18: Right after I start my new class called Graph Commented Apr 13, 2014 at 5:56
  • Could you please post edit your question and add all the code for Graph class 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. Commented Apr 13, 2014 at 5:57

1 Answer 1

2

What you probably want to do in your constructor is to copy the parameters :

//constructor
public Graph(Collection<Vertex> v, Collection<Edge> e){

    V=new ArrayList<Vertex>( v );
    E=new ArrayList<Edge>( e );
}

If you don't want your own copy, but prefer to keep the collection of vertices and edges passed in parameter then you should do what @Bhesh Gurung proposes, but this is less good to my mind as you can't get sure someone won't modify the list of vertices or edges after you build your graph object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.