Java中的“模糊类型”错误是什么? [英] What is an 'ambiguous type' error in Java?

查看:147
本文介绍了Java中的“模糊类型”错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我从最后一行的编译器得到一个错误:'类型列表是不明确的'(在尝试定义cgxHist列表的行上)。我做错了什么?

In the following code, I get an error from the compiler on the last line that says: 'the type List is Ambiguous' (on the line that attempts to define cgxHist list). What am I doing wrong?

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class drawr extends JPanel{

    public static int animationSpeed=470;
    public static int diameter = 50;
    hBod allHBods[];
    List<String> cgxHist = new ArrayList<String>();

我实际上希望列表包含整数,但是当我尝试强制转换列表时,通过用< int> 替换< String> ,该行上的错误变为'令牌上的语法错误int,此标记后预期的维度'。建议请。

I actually wanted the list to contain integers, but when I try to 'cast' the list as such, by replacing <String> with <int>, the error on that line becomes 'Syntax error on token "int", Dimensions expected after this token'. Advice please.

推荐答案

问题是有一个 List 类在 java.awt java.util 包中,当您导入这些包中的所有类时,编译器不知道你的意思。

The problem is that there is a List class in both the java.awt and the java.util package, and as you are importing all classes in those packages, the compiler doesn't know which one you mean.

所以你不应该使用星号同时导入所有类(只需导入你真正需要的类) )或代替 List java.util.List< String> cgxHist = new ArrayList< String>();

So you should either not use the asterisk to import all classes at the same time (just import the ones you actually need) or instead of List write java.util.List<String> cgxHist = new ArrayList<String>();

这篇关于Java中的“模糊类型”错误是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆