ArrayList源码

qq_28938099 2016-11-08 11:04:34
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;//为什么要置为空
return a;
}
这是Arraylist中的一个方法,为什么当传入的数组长度大于arraylist中元素个数时,传入数组index为size的元素要置null
我试着将这句代码拿掉后,并没有什么显见的影响,这句代码是何用途,请大牛指教?
...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ps45221 2016-11-10
  • 打赏
  • 举报
回复
引用 3 楼 qq_28938099 的回复:
[quote=引用 1 楼 Sun1956 的回复:] 同学,遇到问题先去看看API。

    /**
     * <p>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
     * <tt>null</tt>.  (This is useful in determining the length of the
     * list <i>only</i> if the caller knows that the list does not contain
     * any null elements.)
     */
道友,我想了一下,This is useful in determining the length of the * list <i>only</i> if the caller knows that the list does not contain * any null elements. 这句话的意思是【仅 在调用者知道列表中不包含任何 null 元素时才能用此方法确定列表长度】,我有点不理解【这里的 列表长度指的是arraylist的长度吗,这是如何确定的】,希望道友能进一步说明一下,原谅我的愚钝[/quote] 就是你原来的那个list中没有null元素,这样它a[size] = null;赋值后,在确定list长度时才是有用的。如果之前的list当中有null元素,就没办法判断哪个是真的长度了。
qq_28938099 2016-11-09
  • 打赏
  • 举报
回复
引用 1 楼 Sun1956 的回复:
同学,遇到问题先去看看API。

    /**
     * <p>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
     * <tt>null</tt>.  (This is useful in determining the length of the
     * list <i>only</i> if the caller knows that the list does not contain
     * any null elements.)
     */
道友,我想了一下,This is useful in determining the length of the * list <i>only</i> if the caller knows that the list does not contain * any null elements. 这句话的意思是【仅 在调用者知道列表中不包含任何 null 元素时才能用此方法确定列表长度】,我有点不理解【这里的 列表长度指的是arraylist的长度吗,这是如何确定的】,希望道友能进一步说明一下,原谅我的愚钝
qq_28938099 2016-11-09
  • 打赏
  • 举报
回复
引用 1 楼 Sun1956 的回复:
同学,遇到问题先去看看API。

    /**
     * <p>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
     * <tt>null</tt>.  (This is useful in determining the length of the
     * list <i>only</i> if the caller knows that the list does not contain
     * any null elements.)
     */
谢谢提醒,我一定记住您的话
ps45221 2016-11-09
  • 打赏
  • 举报
回复
同学,遇到问题先去看看API。

    /**
     * <p>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
     * <tt>null</tt>.  (This is useful in determining the length of the
     * list <i>only</i> if the caller knows that the list does not contain
     * any null elements.)
     */

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧