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>();
        }
    }
3
  • Sorry should be line 9, this is the problematic line "AdjList = (LinkedList<Integer>[]) new LinkedList[v];". I'm compiling it in the command line. Commented Dec 6, 2016 at 1:37
  • LinkedList does not give great performance for random access. See stackoverflow.com/questions/10656471/… Commented Dec 6, 2016 at 1:40
  • 1
    Possible duplicate of How to create a generic array in Java? Commented Dec 6, 2016 at 1:45

1 Answer 1

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?.
Sign up to request clarification or add additional context in comments.

1 Comment

So should I use a collection instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.