If I am trying to do something like this :

List<?> unknownList = new ArrayList<>();

then the code compiles and runs fine, but of which type the ArrayList has created?

and after this line, If I have done like this :

        unknownList.add("str"); //compilation error

It gives compilation error :

error: no suitable method found for add(String)
        unList.add("str");
              ^
method List.add(int,CAP#1) is not applicable
  (actual and formal argument lists differ in length)
method List.add(CAP#1) is not applicable
  (actual argument String cannot be converted to CAP#1 by method invocation conversion)
method Collection.add(CAP#1) is not applicable
  (actual argument String cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?

What is this error, and is it good to use the diamond-operator with wildcards? If YES then WHERE???

有帮助吗?

解决方案

but of which type was the ArrayList created?

Type parameters are just constraints applied at compile time, but type erasure replaces all occurrences of the type parameter by its erasure (in your case, Object). So, if you're asking about the runtime type, it will be a plain ArrayList (which you can think of as ArrayList<Object>).

is it good to use the diamond-operator with wildcards? If YES then WHERE???

No. When you use a diamond operator to create a new object of a generic class, it means you don't want to be redundant on the type parameter. It should not be combined with a wildcard-declared variable, which does not have a concrete type parameter.

To summarize, you should never write:

List<?> unknownList = new ArrayList<>();

You should only use the wild card <?> when the type really does not matter. In particular, don't use the wild card if you want to add items to the list, because adding items means you know what type to add.

It is likely to be used as a parameter of a method for instance, when you don't access the value and just pass the list on, or where you just access the list items as plain Objects.

其他提示

Your error is that you have a variable of type List<?>, you are calling add on it, and passing a String.

You could do that if your variable were of type List<String> - but since it's only of type List<?>, the compiler has no way of knowing whether it's legitimate to add a String to whatever list the variable references.

If you're going to store strings, then make sure you've got a variable of type List<String> (or some other collection of strings) to store them with.

And it's perfectly fine to use the diamond operator to create something that will be stored in a variable with a wildcard in its type. The diamond operator makes no difference to what you get at run time; and at run time, there are no type parameters. Any subsequent operations you do with the object that you created will be checked at compile time against the type of the variable, not against the line of code that you used to create the object.

but of which type the ArrayList has created?

It could be ArrayList<Object>. It could be ArrayList<String>. It could be ArrayList<SomeBogusUnrelatedClass>. It really doesn't matter, because there's no difference in the compiled code between them anyway, and all you get is a List<?> so you cannot assume anything about what the type parameter is.

is it good to use the diamond-operator with wildcards?

Nope. It does not make sense to have a wildcard-parameterized reference to a newly-created generic object.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top