user3002315
user3002315

Reputation: 91

Implementing Graph in Java, getting raw type error

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>();
        }
    }

Upvotes: 0

Views: 70

Answers (1)

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37916

  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?.

Upvotes: 1

Related Questions