简体   繁体   English

是JDK的错误吗?

[英]Is it bug of JDK?

This code throw 此代码抛出

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:1

on last line. 在最后一行。 Is it bug or what? 是虫子还是什么? (JDK 7) (JDK 7)

int ar[] = {1,2,3};
List arList = Arrays.asList(ar);
arList.set(1,8);

No, Arrays.asList expects Integer [] , but you are passing int[] . 不, Arrays.asList需要Integer [] ,但是您正在传递int[] This should fix it. 这应该解决它。

Integer ar[] = {1,2,3};

This is what my inspection says about using a primitive array where a var-arg type is expected. 这就是我的检查有关使用期望var-arg类型的原始数组的内容。

Reports any calls to a variable-argument method which has a primitive array in in the variable-argument position (eg System.out.printf("%s", new int[]{1, 2, 3}) ). 报告对变量参数方法的任何调用,该方法在变量参数位置处具有原始数组(例如System.out.printf("%s", new int[]{1, 2, 3}) )。 Such a primitive-array argument may be confusing, as it will wrapped as a single-element array, rather than each individual element being boxed, as might be expected. 这样的图元数组参数可能会令人困惑,因为它将包装为单元素数组,而不是像预期的那样将每个单独的元素装箱。

This means that what you have is a List with only one element in it. 这意味着您拥有的是一个仅包含一个元素的列表。 And this element is your int[] . 这个元素就是你的int[]

And you can't access position 1 in this list since there's only one element. 而且,由于只有一个元素,因此您无法访问此列表中的位置1 Thus arList.set(1,8); 因此arList.set(1,8); will throw ArrayIndexOutOfBoundsException . 将抛出ArrayIndexOutOfBoundsException

To avoid this kind of error, never use raw types, instead prefer generic types. 为避免此类错误,请不要使用原始类型,而应使用通用类型。 You want a List of Integers? 您想要一个整数List吗?

Try this: 尝试这个:

int ar[] = {1,2,3};
List<Integer> arList = Arrays.asList(ar); // here
arList.set(1,8);

The compiler will show an error in the line where I wrote the comment, indicating that this will not work. 编译器将在我写注释的行中显示错误,表明这将无法正常工作。 When ar is a primitive array, int[] in your case, then Arrays.asList(ar) will return a List<int[]> . 如果ar是原始数组,在您的情况下为int[] ,则Arrays.asList(ar)将返回List<int[]> List<int[]> and List<Integer> are not compatible. List<int[]>List<Integer>不兼容。 What you have right now is a List of int[] with 1 entry (at index 0), being your array. 您现在拥有的是一个int[] List ,其中有1个条目(在索引0处),是您的数组。

As mentioned before, if you change from primitive int ar[] = {1,2,3}; 如前所述,如果您从原始int ar[] = {1,2,3};更改int ar[] = {1,2,3}; to object Integer ar[] = {1,2,3}; 对象Integer ar[] = {1,2,3}; it will work, because the return type of Arrays.asList(ar) will be List<Integer> . 它将起作用,因为Arrays.asList(ar)的返回类型将为List<Integer>

Source asList : and Collection is not support primitive types asList :并且Collection不支持基本类型

Arrays.asList only expect reference type, like in your case it should be Integer rather then int . Arrays.asList只需要引用类型,就像您的情况一样,应该是Integer而不是int

So you can write your code like following: 因此,您可以像下面这样编写代码:

Integer ar[] = {1,2,3};
List<Integer> arList = Arrays.asList(ar);
arList.set(1,8);

The reason of the specified behavior is clear, but there is nothing about this situation in the javadoc for the Arrays.asList() and no check was made for the primitive arrays as input parameters. 指定行为的原因很明显,但是在Javadoc中没有针对Arrays.asList()的这种情况,也没有检查原始数组作为输入参数。 So, I think this should be treated like a bug. 因此,我认为应该将其视为错误。

Method javadoc: 方法javadoc:

Returns a fixed-size list backed by the specified array. 返回由指定数组支持的固定大小的列表。 (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with {@link Collection#toArray}. (更改为返回列表,将其“写入”到数组。)与{@link Collection#toArray}结合使用,此方法充当基于数组的API和基于集合的API之间的桥梁。 The returned list is erializable and implements {@link RandomAccess}. 返回的列表是可序列化的,并且实现了{@link RandomAccess}。

This method also provides a convenient way to create a fixed-size list initialized to contain several elements 此方法还提供了一种方便的方法来创建固定大小的列表,该列表初始化为包含多个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM