-1

I'm trying to implement a directed graph using Adjacency list where an array of a linked list is used. I'm getting the warning: [unchecked] unchecked conversion but it's running fine.

import java.util.*;

public class traversal{

static class adList{

private int vertices;

private LinkedList<Integer> vertex[];

adList(int V){

    vertices=V;

    vertex = new LinkedList[V];

    for(int i=0;i<V;i++)
        {
            vertex[i] = new LinkedList<Integer>();
        }


            }


public void insertAlist(int start,int end)
{
    //add an edge from source to destination
    vertex[start].addFirst(end);

    //add an edge from dest to source
    //ob.vertex[end].addFirst(start);

}


public void printAlist()
{
    for(int i=0;i<vertices;i++)
        {
            System.out.print("Node "+ i + " HAS THE CONNECTION WITH NODES ");
            for(Integer x:vertex[i])
                    {

                        System.out.print(x+" ");

                    }
            System.out.println();        
        }

}        

}






public static void main(String[] args)
    {
        adList ob = new adList(4);
        ob.insertAlist(2,3);
        ob.insertAlist(2,0);
        ob.insertAlist(0,2);
        ob.insertAlist(0,1);
        ob.insertAlist(1,2);
        ob.insertAlist(3,3);
        ob.printAlist();
    }

}

I'm getting the below warning.

.\traversal.java:15: warning: [unchecked] unchecked conversion
    vertex = new LinkedList[V];
             ^
  required: LinkedList<Integer>[]
  found:    LinkedList[]
1 warning

I tried LinkedList<Integer>[] but it didn't work either. Why am I getting this error? How do I fix this?

suvojit_007
  • 1,690
  • 2
  • 17
  • 23

1 Answers1

-3

You also need to add the generic type to the line

 vertex = new LinkedList[V];

so it should read

 vertex = new LinkedList<Integer>[V];

You can elide the actual type an use the diamond operator in cases where the type system can figure out which type it should be, so you should also be able to do

 vertex = new LinkedList<>[V];

For more information on uncheck typed conversion, check out: Java unchecked conversion

dolan
  • 1,716
  • 11
  • 22