What is an 'ambiguous type' error in Java?

前端 未结 2 414
礼貌的吻别
礼貌的吻别 2021-01-18 03:13

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

相关标签:
2条回答
  • 2021-01-18 04:07
    java.awt.List
    
    java.util.List
    

    Both of these exist. You'll have to add the namespace in front to use one:

    java.util.List<String> cgxHist = new ArrayList<String>();
    

    If you don't, it doesn't know how to interpret the List<T>: is it the awt one or util? Ergo: ambiguous.

    0 讨论(0)
  • 2021-01-18 04:08

    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.

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

    0 讨论(0)
提交回复
热议问题