- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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;
}
这里是AbstractCollection.toArray
方法的实现代码。
if (! it.hasNext()) // fewer elements than expected
return Arrays.copyOf(r, i);
我不明白上面代码的用法。我怀疑代码用于避免在调用方法时更改大小。所以我有两个问题:
最佳答案
好吧,方法的 javadoc 说明了一切:
/**
* {@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>
*/
我发现这里有两件有趣的事情要提:
是的,您是对的,正如 javadoc 所说,即使同时修改了 Collection,此方法也准备好正确返回。这就是为什么初始大小只是一个提示。迭代器的使用还确保避免“并发修改”异常。
很容易想象一种多线程情况,其中一个线程从集合中添加/删除元素,而另一个线程调用集合中的“toArray”方法。在这种情况下,如果 Collection 不是线程安全的(比如通过 Collections.synchronizedCollection(...)
方法获得,或者通过手动创建同步访问代码),你会遇到这样的情况它被同时修改和 toArray-ed。
关于java - AbstractCollection 的 toArray 方法实现中代码的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401644/
AbstractCollection 实现了 Collection。那么为什么 AbstractCollection 在那里,为什么我们使用 Collection 而不是直接使用 AbstractCo
我查看了abstraccollection,发现containsAll方法会迭代整个集合。为什么此方法不检查集合的大小?如果主集合只有一个元素而目标集合有 100000 个元素,性能将会下降。 Arr
这是一 block AbstractCollection类: public boolean addAllm(Collection c) { boolean modified = false;
AbstractCollection 中有一段奇怪的代码: public Object[] toArray() { // Estimate size of array; be prepared
发件人:Collections Framework “AbstractCollection 类提供了所有方法的实现,除了 iterator() 和 size() 方法,它们在适当的子类中实现” 谁能说
public boolean contains(Object o) { Iterator it = iterator(); if (o==null) {
你知道吗: Map m1 = new HashMap(); Map m2 = new HashMap(); System.out.println("m1.equals(m2) = "+m1.equal
当对 AbstractCollection 进行子类化时,我仍然必须实现 size(),即使(我相信)有一个 合理的正确的(尽管性能不佳)默认实现: public int size() { i
public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements
public abstract class AbstractCollection implements Collection { public boolean add(E e) {
我想知道为什么要创建 Collections 类。从理论上讲,此类中的方法可以放在 AbstractCollection 类中。那么创建单独的 utils 类的原因是什么? 最佳答案 并非每个系列都扩
AbstractCollection 实现了 Iterable 和 Collection 接口(interface)。但是,Collection 是 Iterable 的子接口(interface)。
Iterable<T> 是Collection(List、Set、Queue)的顶级接口,api释义:实现这个接口允许对象成为 "foreach" 语句的目标。 可
您好,我在 intellij idea 中运行了包含这些说明的代码 SootClass c = Scene.v().loadClassAndSupport(name); final Body b =
这个问题已经有答案了: Why is there a method iterator() on java.util.Collection (4 个回答) 已关闭 9 年前。 当迭代器 iterator
我已经开始使用 C++ 编写代码,来自 Java 背景(实际上我在我的大学学习了 C++,但我们从未接触过 STL 等) 无论如何,我已经到了在各种集合中排列数据的地步,我立即告诉自己“好吧,这是一种
我是一名优秀的程序员,十分优秀!