英文:
Why is arr[6] returning a null value in this code?
问题
ArrayList
list1.add("Hey");
list1.add("there");
list1.add(0, "knock");
list1.add(0, "knock");
list2.addAll(list1);
list3.add("Vasvi");
list3.add(0, "I'm");
list2.addAll(2, list3);
String[] arr = new String[8];
arr[6] = "Who're";
arr[7] = "You?";
System.out.println("arr[6]=" + arr[6]);
arr = list2.toArray(arr);
System.out.println("arr[6]=" + arr[6]);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Output:
arr[6]=Who're
arr[6]=null
knock
knock
I'm
Vasvi
Hey
there
null
You?
As soon as the toArray(arr) method is called arr[6] becomes null. I'm not able to understand why?
英文:
ArrayList<String> list1=new ArrayList<String>(),list2 = new ArrayList<String>(),list3= new ArrayList<String>();
list1.add("Hey");
list1.add("there");
list1.add(0, "knock");
list1.add(0, "knock");
list2.addAll(list1);
list3.add("Vasvi");
list3.add(0, "I'm");
list2.addAll(2, list3);
String[] arr= new String[8];
arr[6]="Who're";
arr[7]="You?";
System.out.println("arr[6]="+arr[6]);
arr=list2.toArray(arr);
System.out.println("arr[6]="+arr[6]);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Output :
>arr[6]=Who're
arr[6]=null
knock
knock
I'm
Vasvi
Hey
there
null
You?
As soon as the toArray(arr) method is called arr[6] becomes null. I'm not able to understand why?
答案1
得分: 3
因为toArray(T[] a)
的实现包含以下语句:
if (a.length > size)
a[size] = null;
这是因为在这种情况下,size
为6,a.length
为8,因此a[6] = null
,这正是您观察到的现象。
文档解释如下:
如果列表适合指定的数组并且有多余的空间(即,数组的元素比列表多),则紧随集合结束的数组元素将设置为
null
。(仅当调用方知道列表不包含任何null
元素时,这对于确定列表的长度非常有用。)
英文:
Because the implementation of toArray(T[] a)
has the statements
if (a.length > size)
a[size] = null;
That is the case here since size
is 6 and a.length
is 8 and therefore a[6] = null
which is exactly what you observe.
The docs explain:
>If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null
. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
答案2
得分: 2
不需要查看实现;查看文档就足够了:
> 如果列表适合指定的数组并且还有空余空间(即,数组的元素比列表多),则在集合结束后紧接着数组中的元素将设置为null。
英文:
No need to look at the implementation; a look at the documentation is sufficient:
> If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null.
答案3
得分: 1
toArray(T[] a)
方法的工作方式最好理解的方法是花点时间阅读 javadocs:
如果列表适合指定的数组且还有剩余空间(即,数组的元素多于列表),列表末尾后面的数组元素将被设置为 null。(只有在调用者知道列表不包含任何 null 元素时,这对于确定列表的长度非常有用。)
在您的示例中,数组有8个元素,而列表只有6个。因此,第6个元素是 null
。
英文:
The best way to understand how toArray(T[] a)
works is to take a moment and read javadocs:
> If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
In your example array has 8 element and list only 6. Because of that 6th element is null
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论