gpt4 book ai didi

java - "Cannot instantiate the type..."

转载 作者:IT老高 更新时间:2023-10-28 21:20:47 28 4
gpt4 key购买 nike

当我尝试运行这段代码时:

import java.io.*;
import java.util.*;

public class TwoColor
{
public static void main(String[] args)
{
Queue<Edge> theQueue = new Queue<Edge>();
}

public class Edge
{
//u and v are the vertices that make up this edge.
private int u;
private int v;

//Constructor method
public Edge(int newu, int newv)
{
u = newu;
v = newv;
}
}
}

我收到此错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     Cannot instantiate the type Queue    at TwoColor.main(TwoColor.java:8)

我不明白为什么我不能实例化这个类...这对我来说似乎是正确的...

最佳答案

java.util.Queue 是一个接口(interface),所以你不能直接实例化它。你可以实例化一个具体的子类,比如LinkedList:

Queue<T> q = new LinkedList<T>;

关于java - "Cannot instantiate the type...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813694/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com