public class Test {
public static<T> void toTest(Collection<T> x,Collection<T> y){
for(T a:x){
y.add(a);
}
}
public static void main(String[] args) {
List<Object> ax = new ArrayList<>();
List<String> ay = new ArrayList<>();
toTest(ax, ay);//报错如下:
//The method toTest(Collection<T>, Collection<T>)
//in the type Zhaocuo is not applicable for the arguments (List<Object>, List<String>)
}
}
如上代码,为什么报这种错误?上面那个 T 是啥意思?static<T>这么写是啥意思?
求大神解释一下,谢谢!
public static<T> void toTest(Collection<T> x,Collection<T> y){
for(T a:x){
y.add(a);
}
}
public static void main(String[] args) {
List<Object> ax = new ArrayList<>();
List<String> ay = new ArrayList<>();
toTest(ax, ay);//报错如下:
//The method toTest(Collection<T>, Collection<T>)
//in the type Zhaocuo is not applicable for the arguments (List<Object>, List<String>)
}
}
如上代码,为什么报这种错误?上面那个 T 是啥意思?static<T>这么写是啥意思?
求大神解释一下,谢谢!
for (T2 a : y) {
x.add(a);
}
}
public static void main(String[] args) {
List<Object> ax = new ArrayList<>();
List<String> ay = new ArrayList<>();
ay.add("b");
ay.add("c");
toTest(ax, ay);
System.out.println(ax);
}
}
2)报错是因为String与Object不是同一类的实例