Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an 'ambiguous type' error in Java?

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

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.

like image 902
user2671186 Avatar asked Nov 27 '13 23:11

user2671186


People also ask

What is ambiguous error in Java?

Ambiguity errors occur when erasure causes two seemingly distinct generic declarations to resolve to the same erased type, causing a conflict. Here is an example that involves method overloading: Notice that MyGenClass declares two generic types: T and V.

What is the ambiguity error?

[‚am·bə′gyü·əd·ē ‚er·ər] (computer science) An error in reading a number represented in a digital display that can occur when this representation is changing; for example, the number 699 changing to 700 might be read as 799 because of imprecise synchronization in the changing of digits.

What is ambiguity error in method overloading?

There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn't have the knowledge as to which method to call.

What is ambiguous invocation in Java explain with example?

Ambiguous Invocation. □ Sometimes there may be two or more possible. matches for an invocation of a method, but the. compiler cannot determine the most specific match. This is referred to as ambiguous invocation.


1 Answers

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

like image 112
Blub Avatar answered Sep 20 '22 16:09

Blub