-1

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
  • You should specify the generic type (`List`) or "allow" all types with `List extends Object>`. But the second version has no benefit so you should prefer the first one. – Stefan Warminski Mar 23 '17 at 09:40
  • @PJvG He can compile `List> list1 = new ArrayList>();`successfully but as Slimu answered you can only add `null` to a list with an unbounded wildcard type. – Stefan Warminski Mar 23 '17 at 09:42
  • @StefanWarminski right, I worded it wrong, I meant he cannot do `List> list1 = new ArrayList>();` followed by `list1.add("Zahid");`. I will delete my comment because Slimu's answer is sufficient enough to explain the problem. – PJvG Mar 23 '17 at 09:45

2 Answers2

1

You are using a generic collection with an unbound wildcard type. This means you can only iterate over that collection and the only value you can add is null.

See this tutorial if you wish to learn more: https://docs.oracle.com/javase/tutorial/java/generics/

Slimu
  • 2,301
  • 1
  • 22
  • 28
0

when you're saying

new ArrayList<>();

basically What you're trying to do is Instantiate a ArrayList.

[?] of Type WildCard character. wildCard [?] means a one specific yet unknown type. Since that type is not known compile-time, you're not allowed to modify the list.

if you see the implementation of arrayList in java.util there are Generics inside the arrayList which are expecting a type.

So when you use wildCard "?" Compiler has no idea Which type of ArrayList to instantiate. It will throw a compile time error "Cannot instantiate the type ArrayList"

so when you're adding something. it is more likely will ask you to add null to the list .

To know more about wildCards : [https://stackoverflow.com/a/12341269/7756013]

Community
  • 1
  • 1
im_chiru
  • 1
  • 1