简体   繁体   English

在Java中实现图,出现原始类型错误

[英]Implementing Graph in Java, getting raw type error

I'm trying to implement a Graph as an adjacency list, with the code below. 我正在尝试使用下面的代码将Graph实现为邻接表。 I'm getting a raw type error on line 9. 我在第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. 编辑:只是想澄清一下,我得到一个未经检查/不安全的操作错误,然后当我用Xlint编译时,我得到了原始类型错误。

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>();
        }
    }
  1. There's unsafe typecast in line 9. 第9行中存在不安全的类型转换。
  2. Do not mix arrays and generics: LinkedList<Integer>[] is a bad smell. 不要混合使用数组和泛型: LinkedList<Integer>[]有异味。 And that's the real problem. 那才是真正的问题。 You can't have arrays of generic classes. 您不能有泛型类的数组。 Java simply doesn't support it. Java根本不支持它。 Read more in Q: How to create a generic array in Java? Q:如何用Java创建通用数组中阅读更多内容 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM