I am confusing that if the following method are same? Are there any subtle differences?
Your advice are very appreciated.
method 1
public static double sumOfList(List<?> list) {}
Run Code Online (Sandbox Code Playgroud)
method 2
public static <T> double sumOfList(List<T> list) {}
Run Code Online (Sandbox Code Playgroud)
在方法内部,您可以T 在第二种情况下使用.
你不能在第一种情况下.
考虑这个例子
private static <T> void printList(List<T> list) {
for (T t: list) {
System.out.println(t);
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
3815 次 |
| 最近记录: |