我主要是一名C#开发者,我正在教授数据结构给我的朋友,但他们在大学使用Java,在Java中我看到了这样的表达式:
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
我在C#中没有见过这样的东西,所以我想知道Collection<T>
和Java中的Collection<?>
有什么区别?
void printCollection(Collection<T> c) {
for (Object e : c) {
System.out.println(e);
}
}
我认为它也可以按照上面的方式编写。但是文档中的人在比较Collection<Object>
和Collection<T>
。
示例来自http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
void printCollection(Collection<T> c)
还是<T> void printCollection(Collection<T> c)
?后者是一种泛型方法。前者不是泛型方法,必须在具有类型参数T
的泛型类中声明。 - newacct