在AbstractCollection的toArray方法的实现中的代码的用法是什么 [英] what's the usage of the code in the implementation of AbstractCollection's toArray Method

查看:249
本文介绍了在AbstractCollection的toArray方法的实现中的代码的用法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public Object [] toArray(){
//估计数组的大小;准备看到更多或更少的元素
Object [] r = new Object [size()];
迭代器< E> it = iterator();
for(int i = 0; i if(!it.hasNext())//少于预期的元素
return Arrays.copyOf , 一世);
r [i] = it.next();
}
return it.hasNext()? finishToArray(r,it):r;
}

这是代码实现 AbstractCollection.toArray 方法。

  if(!it.hasNext())//少于预期的元素
return Arrays.copyOf(r,i);

我不明白上面的代码的用法。我怀疑代码是用来避免大小改变时调用方法。所以我有两个问题:


  1. 我怀疑是对还是错?

  2. 如果是真的,在方法被调用时,什么情况可以改变大小?
    如果这是错误的, ol>

    解决方案

    好吧,方法的javadoc sais一切:

      / ** 
    * {@inheritDoc}
    *
    *< p>此实现返回一个数组,包含此集合返回的所有元素
    *迭代器,以相同的顺序存储在
    *数组的连续元素中,以index {@code 0}开头。
    *返回数组的长度等于迭代器返回的元素数量
    *,即使此集合的大小在迭代期间更改为
    *,也可能发生在集合允许$​​ b $ b *在迭代期间同时修改。 {@code size}方法是
    *仅作为优化提示调用;正确的结果返回
    *,即使迭代器返回不同数量的元素。
    *
    *< p>此方法等效于:
    *
    *< pre> {@code
    * List< E> list = new ArrayList< E>(size());
    * for(E e:this)
    * list.add(e);
    * return list.toArray();
    *}< / pre>
    * /

    我发现两个有趣的事情:


    1. 是的,你是对的,作为javadoc sais,这个方法准备好返回正确,即使集合在同一时间被修改。这就是为什么初始尺寸只是一个提示。


    2. 这很容易想象一个多线程的情况,其中一个线程添加/删除元素从一个集合,而不同的线程调用toArray方法。在这种情况下,如果Collection不是线程安全的(像通过 Collections.synchronizedCollection(...)方法获得,或者通过手动创建同步的访问代码)



    public Object[] toArray() {
        // Estimate size of array; be prepared to see more or fewer elements
        Object[] r = new Object[size()];
        Iterator<E> it = iterator();
        for (int i = 0; i < r.length; i++) {
            if (! it.hasNext()) // fewer elements than expected
                return Arrays.copyOf(r, i);
            r[i] = it.next();
        }
        return it.hasNext() ? finishToArray(r, it) : r;
    }
    

    here's the code of implementation of AbstractCollection.toArray method.

    if (! it.hasNext()) // fewer elements than expected
        return Arrays.copyOf(r, i);
    

    I don't understand the usage of the code above. I suspect the code is used to avoid the size changing while the the method is invoked. So I have two questions:

    1. What I suspect is right or wrong? if it's wrong, what's the usage of this code?
    2. If it's true, what situation can make the size changing while the method has been invoked?

    解决方案

    Well, the method's javadoc sais it all:

     /**
         * {@inheritDoc}
         *
         * <p>This implementation returns an array containing all the elements
         * returned by this collection's iterator, in the same order, stored in
         * consecutive elements of the array, starting with index {@code 0}.
         * The length of the returned array is equal to the number of elements
         * returned by the iterator, even if the size of this collection changes
         * during iteration, as might happen if the collection permits
         * concurrent modification during iteration.  The {@code size} method is
         * called only as an optimization hint; the correct result is returned
         * even if the iterator returns a different number of elements.
         *
         * <p>This method is equivalent to:
         *
         *  <pre> {@code
         * List<E> list = new ArrayList<E>(size());
         * for (E e : this)
         *     list.add(e);
         * return list.toArray();
         * }</pre>
         */
    

    I find two interesting things to mention here:

    1. Yes, you're right, as the javadoc sais, this method is prepared to return correctlly even if the Collection has been modified in the mean time. That's why the initial size is just a hint. The usage of the iterator also ensures avoidance from the "concurrent modification" exception.

    2. It's very easy to imagine a multi-threaded situation where one thread adds/removes elements from a Collection while a different thread calls the "toArray" method on it. In such a situation, if the Collection is not thread safe (like obtained via Collections.synchronizedCollection(...) method, or by manually creating synchronized access code towards it) you'll get into a situation where it's modified and toArray-ed at the same time.

    这篇关于在AbstractCollection的toArray方法的实现中的代码的用法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆