2021-06-27

Error : Type mismatch: cannot convert from List

package set01;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DFTList {
    static List<Integer>[]   list;
    static boolean[] visited;
    
    public DFTList(int nodes) {
        list = new ArrayList[nodes];
        visited = new boolean[nodes];
        for(int i=0;i<nodes;i++) {
           list[i] = new ArrayList<>();
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter no. of nodes : ");
        int nodes = scan.nextInt();
        DFTList obj = new DFTList(nodes);
        System.out.println("Enter no.of vertex : ");
        int vertex = scan.nextInt();
        
        
        for(int i=0;i<vertex;i++) {
            int v1 = scan.nextInt();
            int v2 = scan.nextInt();
            list[v1].add(v2);
            list[v2].add(v1);
        }
        
        solve(0);
    }
    
    public static void solve(int a) {
        visited[a] = true;
        ArrayList<Integer> l = list[a];
    }

}

In the above code snippet, At the DFTList constructor, I have inserted ArrayList object at all the indices of the array. But when I try to retrieve the same object at the solve method and store it under the same reference, I encountered an error stating that "Type mismatch: cannot convert from List to ArrayList". Why does this error occur?



from Recent Questions - Stack Overflow https://ift.tt/35TWOyh
https://ift.tt/eA8V8J

No comments:

Post a Comment