Using a List and importing java.awt.* at the same time

2.2k Views Asked by At

Whenever I import java.awt.* and try to use a List I get an error.

Is there any way to import java.awt.* and use a List?

3

There are 3 best solutions below

0
Kumar Vivek Mitra On BEST ANSWER

There is an ambiguity in the naming conventions of List in class awt and util, so we can handle it in 2 ways:

  1. Use import

    import java.awt.*;

    import java.util.List;

    import java.util.*;

  2. Use the full path, as mentioned in Head First Java,"either use import or the full name"

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

0
Antimony On

From a quick Google search

The class name List is now ambiguous because there are two classes java.awt.List and java.util.List. You can resolve the ambiguity by adding a specific import of the class name:

import java.awt.*;
import java.util.*;
import java.util.List;

However, if you need to refer to both java.awt.List and java.util.List in the same source file, then you have crossed the limits of the import mechanism. You can use an import statement to shorten one of the names to List, but you need to reference the other by its full name whenever it occurs in the source text.

0
RoneRackal On

The problem is java.awt also has a List inside it, so the compiler doesn't know which one you are using when you call List.

I think you'll have call your list like so:

java.util.List list = new java.util.List();

So this way the compiler knows which 'List' you are referring to.