arr[6]在这段代码中返回null值的原因是什么?

huangapple go评论141阅读模式
英文:

Why is arr[6] returning a null value in this code?

问题

ArrayList list1 = new ArrayList(), list2 = new ArrayList(), list3 = new 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&lt;String&gt; list1=new ArrayList&lt;String&gt;(),list2 = new ArrayList&lt;String&gt;(),list3= new ArrayList&lt;String&gt;();
list1.add(&quot;Hey&quot;);
list1.add(&quot;there&quot;);
list1.add(0, &quot;knock&quot;);
list1.add(0, &quot;knock&quot;);
list2.addAll(list1);
list3.add(&quot;Vasvi&quot;);
list3.add(0, &quot;I&#39;m&quot;);
list2.addAll(2, list3);
		
String[] arr= new String[8];
		
		
arr[6]=&quot;Who&#39;re&quot;;
arr[7]=&quot;You?&quot;;
System.out.println(&quot;arr[6]=&quot;+arr[6]);
		
arr=list2.toArray(arr);
System.out.println(&quot;arr[6]=&quot;+arr[6]);
		
for (int i = 0; i &lt; 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 &gt; 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.

huangapple
  • 本文由 发表于 2020年7月30日 15:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63168264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定