0

I'm trying to implement a Graph as an adjacency list, with the code below. I'm getting a raw type error on line 9.

Edit: Just want to clarify, I get an unchecked/unsafe operation error, then when I compile with Xlint, I get the raw type error.

import java.util.LinkedList;

public class AdjListGraph{
    private int vertices;
    private LinkedList<Integer>[] AdjList;

    public AdjListGraph(int v){
        this.vertices = v;
        AdjList = (LinkedList<Integer>[]) new LinkedList[v];
        for (int i = 0; i < v; i++){
            AdjList[i] = new LinkedList<Integer>();
        }
    }
  • Sorry should be line 9, this is the problematic line "AdjList = (LinkedList[]) new LinkedList[v];". I'm compiling it in the command line. – user3002315 Dec 06 '16 at 01:37
  • LinkedList does not give great performance for random access. See http://stackoverflow.com/questions/10656471/performance-differences-between-arraylist-and-linkedlist – dnault Dec 06 '16 at 01:40
  • 1
    Possible duplicate of [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Tim Biegeleisen Dec 06 '16 at 01:45

1 Answers1

1
  1. There's unsafe typecast in line 9.
  2. Do not mix arrays and generics: LinkedList<Integer>[] is a bad smell. And that's the real problem. You can't have arrays of generic classes. Java simply doesn't support it. Read more in Q: How to create a generic array in Java?.
Community
  • 1
  • 1
naXa
  • 26,677
  • 15
  • 154
  • 213