Maddy
Maddy

Reputation: 2060

Adding vertices to graph using ArrayList

I am trying to add create two ArrayList of vertices (setA, setB) which will store the stores for comparing them late how ever I am unable to add vertices to the arraylist.

here is the code

import java.util.*;


public class BipartiteGraph<Vertex> {

    private String strName;
    ArrayList<Vertex>[] vertexList;

    public BipartiteGraph(){

        vertexList = new ArrayList[2];
        vertexList[0] = new ArrayList<Vertex>();
        vertexList[1] = new ArrayList<Vertex>();
        Scanner vertexInput = new Scanner(System.in);
        int vertex;
        vertex = vertexInput.nextInt();
        for(int i = 0; i < 10; i++){
            vertexList[0].add(vertexInput.nextInt());
        }
    }
}

Also if someone could guide me if I am in the right direction.

Upvotes: 0

Views: 1504

Answers (1)

lejlot
lejlot

Reputation: 66815

You are trying to add int variable into the container of Vertex objects. Assuming, that your Vertex has a constructor accepting int you should rather use:

vertexList[0].add(new Vertex(vertexInput.nextInt()));

Upvotes: 1

Related Questions