java方法在类型列表中添加的(对象)不适用于参数(int)意味着什么?
我得到以下错误:
the method add(object) in the type list is not applicable for the arguments (int)
对于此代码:
import java.util.ArrayList;
import java.util.Collection;
public class CollectionList {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(1);
c.add("siddharth");
c.add(4.3);
for(object o)
}
}
# 1 楼答案
当您在代码中创建非泛型ArrayList时,它应该存储对象,而对象是引用类型,当您尝试添加int(一种值类型)时,它会失败
你能做的就是把你的数字转换成整数类型,然后它就可以工作了
# 2 楼答案
一般来说,java集合(泛型和非泛型)不适用于原语。必须使用引用类型才能使其可用于集合