为什么这些在Java中无效?
发布时间:2020-12-15 05:23:06 所属栏目:Java 来源:网络整理
导读:ListObject myList = new ArrayListString(); //(hint: no)MapInteger myMap = new HashMapint(); // (hint: also no) 为什么上述声明中的陈述有误? 解决方法 我们来看第一个例子.考虑一下您应该能够在List Object上执行的操作:添加,删除,检索任何对象. 您
List<Object> myList = new ArrayList<String>(); //(hint: no) Map<Integer> myMap = new HashMap<int>(); // (hint: also no) 为什么上述声明中的陈述有误? 解决方法
我们来看第一个例子.考虑一下您应该能够在List< Object>上执行的操作:添加,删除,检索任何对象.
您正尝试使用可以添加,删除和仅检索字符串的集合来填充这些需求. List<Object> myList = new ArrayList<String>(); // This should be valid based on the List<Object> interface but obviously // MyClass isn't String...so what would the statement do? myList.add(new MyClass()); 第二个原因仅仅是因为Java中的泛型不支持基本类型.有关更多信息,您可以查看: java – Why don’t Generics support primitive types? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |