Skip to content
Advertisement

Incompatible types : Int cannot be converted to “class” data type

I’m trying to insert value to a List using loop for, but this list data type is a class which have constructor in it. Here is my code :

List<Edge> edge= Arrays.asList();
    for(int i=0;i<M;i++){
        System.out.println("insert first vertex that connected to edge : ");
        x=key.nextInt();
        System.out.println("insert second vertex that connected to edge : ");
        y=key.nextInt();
        edge.add(x,y);
    }

And the error message on line edge.add(x,y) it says :

Incompatible data types : int cannot be converted to Edge

Here is the Edge Class :

public class Edge{
    int source, destination;
    public Edge(int source, int destination){
        this.source=source;
        this.destination=destination;
    }
}

What should i change? Thank you.

Advertisement

Answer

Changing edge.add(x,y) to edge.add(new Edge(x,y)) should help. The list edge expects an Object of class Edge, while two ints are being passed, causing the error.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement