问题
What does List<?>
mean, does it mean simply a list of objects of unspecified type?
Googling for the string <?>
returns nothing useful (:
回答1:
The keyword you need to get more information is Wildcards
回答2:
As Tom said, the ?
, or unbounded wildcard, means that the type of the object is not specified. It could be unknown, could be meant for multiple possible values or might be just plain irrelevant. Your example, List<?>
, is pronounced "List of unknown." It's convenient because it's flexible, but there are also some pitfalls because you can't shove random objects in and pull them out of groups of unknown with total impunity.
Resources:
- Wildcards are discussed here in the Java tutorial.
- There's a good -- if verbose -- tutorial on generics in general by Angelika Langer available here.
- And there's another good overview here (PDF) by Gilad Bracha; check out pages 5-7.
- Finally, if you can get your hands on Effective Java by Josh Bloch, it's got a great section on generics and the cases in which you can, can't, should and shouldn't use wildcards (chapter 5, pages 109-146 in the second edition).
Incidentally, your Google search failed because Google doesn't truck with special characters:
With some exceptions, punctuation is ignored (that is, you can't search for @#$%^&*()=+[]\ and other special characters).
-Google help page
(EDIT: I must have been really tired when I wrote this last night. Cleaned up formatting/added a little info.)
回答3:
To answer this question I need to explain Unbounded Wildcards and Bounded Wildcards.
The content of this post has been assembled from java documentation.
1. Unbounded Wildcards
The unbounded wildcard type is specified using the wildcard character (
?
), for example,List<?>
. This is called a list of unknown type. There are two scenarios where an unbounded wildcard is a useful approach:
If you are writing a method that can be implemented using functionality provided in the Object class.
When the code is using methods in the generic class that don't depend on the type parameter. For example,
List.size
orList.clear
. In fact,Class<?>
is so often used because most of the methods inClass<T>
do not depend onT
.
2. Bounded Wildcards
Consider a simple drawing application that can draw shapes such as rectangles and circles. To represent these shapes within the program, you could define a class hierarchy such as this:
public abstract class Shape {
public abstract void draw(Canvas c);
}
public class Circle extends Shape {
private int x, y, radius;
public void draw(Canvas c) {
...
}
}
public class Rectangle extends Shape {
private int x, y, width, height;
public void draw(Canvas c) {
...
}
}
These classes can be drawn on a canvas:
public class Canvas {
public void draw(Shape s) {
s.draw(this);
}
}
Any drawing will typically contain a number of shapes. Assuming that they are represented as a list, it would be convenient to have a method in Canvas that draws them all:
public void drawAll(List<Shape> shapes) {
for (Shape s: shapes) {
s.draw(this);
}
}
Now, the type rules say that
drawAll()
can only be called on lists of exactly Shape: it cannot, for instance, be called on aList<Circle>
. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on aList<Circle>
. What we really want is for the method to accept a list of any kind of shape: public void drawAll(List shapes) { ... } There is a small but very important difference here: we have replaced the typeList<Shape>
withList<? extends Shape>
. NowdrawAll()
will accept lists of any subclass ofShape
, so we can now call it on aList<Circle>
if we want.
List<? extends Shape>
is an example of a bounded wildcard. The?
stands for an unknown type, however, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.
Similarly, the syntax ? super T
, which is a bounded wildcard, denotes an unknown type that is a supertype of T.
A ArrayedHeap280<? super Integer>
, for example, includes ArrayedHeap280<Integer>
, ArrayedHeap280<Number>
, and ArrayedHeap280<Object>
.
As you can see in the java documentation for Integer class, Integer is a subclass of Number that in turn is a subclass of Object.
回答4:
Sounds like you should look for some documentation on Java generics.
The List<?>
means that it is an object based on a currently unspecified type. That specification is made when the class is instantiated.
For example:
List<String> listOfStrings = new ArrayList<String>();
is a list of String objects.
回答5:
List
is an interface you can implement yourself and also implemented by some of the Java collections, like Vector
.
You can provide compile-time typing information using the angled brackets. The most generic type would be Object
, which would be List<Object>
. The <?>
you see is indicating a List of some subclass of Object
or an Object
. This is like saying List<? extends Object>
, or List<? extends Foo>
, where the List
contains objects of some subclass of Foo
or objects of Foo
itself.
You can't instantiate a List
; it's an interface, not an implementation.
回答6:
List<?>
stands for List<? extends Object>
so in Collection<E>
you will find containsAll(Collection<?> c)
which allows you to write
List<Object> objs = Arrays.<Object>asList("one",2,3.14,4);
List<Integer> ints = Arrays.asList(2,4);
assert objs.containsAll(ints);//true
回答7:
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.
Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
chk dis pdf
回答8:
? is nothing but Wildcard in Generics
There are 3 different kind of Wildcards in Generics
1) Upper Bounded Wildcards: Uses extends key word
eg: List<? extends SuperClass>
2) Lower Bounded Wildcards
eg:Uses Super key word List<? super SubClass>
3) Unbounded Wildcard
List<?> list
回答9:
You are probably looking at the template based List
class. You can create a list of strings by List<String> myList = new MyList<String>();
as an example. Check the documentation for all the types it supports. It should support any object type, but if there is a sort functionality you have to supply some compare functions.
Note that in the example above MyList
is a concrete class that implements the List
interface in Java. It can be ArrayList
.
EDIT:
I assumed List
as a concrete class by mistake. Fixed the error above. Thanks Jon.
来源:https://stackoverflow.com/questions/1844770/what-does-list-mean-in-java-generics