I was writing small piece of code for Collections using Generics and the code is as Below.
import java.util.*;
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
List<?> list1 = new ArrayList<?>();
list1.add("Zahid");
list1.add(22);
System.out.print(list1.toString());
}
}
Can Someone explain what is wrong with this code. Because it is throwing following error.
error: unexpected type
List<?> list1 = new ArrayList<?>();
^
required: class or interface without bounds
found: ?
HelloWorld.java:11: error: no suitable method found for add(String)
list1.add("Zahid");
^
method Collection.add(CAP#1) is not applicable
(argument mismatch; String cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
(argument mismatch; String cannot be converted to CAP#1)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
HelloWorld.java:12: error: no suitable method found for add(int)
list1.add(22);
^
method Collection.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors