为什么Java泛型适用于基元数组,但不适用于基元数组?
ArrayList<int> list1 = new ArrayList<int>(); // Error
ArrayList<int[]> list2 = new ArrayList<int[]>(); // Works fine
泛型不适用于基元类型,但适用于基元数组。为什么
跟进:
我能想到它的唯一原因是因为数组是一个对象,所以泛型可以与它们一起工作。但如果是这样的话,为什么不能将原始int数组自动装箱到相应的整数包装类中呢
Integer arr = new int[5]; // Error
Integer[] arr = new int[5]; // Error
Integer a = 5; // Works fine (autoboxing)
如果有人能阐明这一点,我将不胜感激,或者如果我遗漏了它在内部的表现方式。谢谢
共 (0) 个答案