ArrayList理解
2016-03-10 00:00
148 查看
/** * Resizable-array implementation of the <tt>List</tt> interface. Implements * 可调整的数组实现了List接口 * all optional list operations, and permits all elements, including * 实现所有的可选择的list操作并且允许所有元素的添加,包括null * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, * 除实现List接口外 * this class provides methods to manipulate the size of the array that is * 这个类提供方法来操作数组的大小 * used internally to store the list. (This class is roughly equivalent to * 用于内部存储列表 * <tt>Vector</tt>, except that it is unsynchronized.)<p> * (除了不同步之外,此类和Vector相当.) * The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>, * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant * Size,isEmpty,get,set,iterator,listIterator操作运行在常量时间 * time. The <tt>add</tt> operation runs in <i>amortized constant time</i>, * add操作运行在摊销常量时间 * that is, adding n elements requires O(n) time. All of the other operations * 换句话说,添加n个对象必须看添加每个对象的复杂度(O(n)这个大O表示的是最坏情况下的时间复杂度) * run in linear time (roughly speaking). The constant factor is low compared * 其他的操作运行在线性时间.(理解:add方法和添加对象的复杂程度有关系,而其他方法不存在复杂程度问题) * to that for the <tt>LinkedList</tt> implementation.<p> * 常量因素比LinkedList实现是低的。 * Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is * 每个ArrayList初始化有一个容量。 * the size of the array used to store the elements in the list. It is always * 容量被用于数组存储对象列表的大小。 * at least as large as the list size. As elements are added to an ArrayList, * 总是比列表的大小大的。比如对象添加到一个ArrayList * its capacity grows automatically. The details of the growth policy are not * 它的容量是自动增长。 * specified beyond the fact that adding an element has constant amortized * time cost.<p> * 对于增加一个对象的摊销时间成本,增长的原则没有超过事实情况(只容量增长时间成本是可控的) * * An application can increase the capacity of an <tt>ArrayList</tt> instance * before adding a large number of elements using the <tt>ensureCapacity</tt> * operation. This may reduce the amount of incremental reallocation. * 添加大量的对象使用ensureCapacity方法。可以减少容量增加后的重新分配。(否则每次add等就会调用ensureCapacity) * * <p><strong>Note that this implementation is not synchronized.</strong> * 注意:ArrayList的实现过程是不同步的(线程不安全的,不能再多线程环境下使用) * If multiple threads access an <tt>ArrayList</tt> instance concurrently, * 如果多线程访问一个ArrayList实例。 * and at least one of the threads modifies the list structurally, it * 并且至少有一个线程修改list结构, * <i>must</i> be synchronized externally. (A structural modification is * 它必须在外边做同步 * any operation that adds or deletes one or more elements, or explicitly * resizes the backing array; merely setting the value of an element is not * a structural modification.) This is typically accomplished by * (结构性修改指操作 添加或删除对象或明确的调整大小;只是设置对象的值不是结构性修改) * synchronizing on some object that naturally encapsulates the list. * 典型的操作方式是同步一些对象,完成相应的封装。(理解:如果多线程访问ArrayList,必须对ArrayList对象做相应同步封装操作) * If no such object exists, the list should be "wrapped" using the * 如果不存在这些对象,list应该包装为 * {@link Collections#synchronizedList Collections.synchronizedList} * method. This is best done at creation time, to prevent accidental * 这是最好的方式阻止意外的不同步访问list * unsynchronized access to the list:<pre> * List list = Collections.synchronizedList(new ArrayList(...));</pre> * * <p>The iterators returned by this class's <tt>iterator</tt> and * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is * 迭代器的返回通过该类的iterator和listIterator方法是错误的机制。 * structurally modified at any time after the iterator is created, in any way * 如果迭代器创建之后list结构被修改了 * except through the iterator's own <tt>remove</tt> or <tt>add</tt> methods, * 可能通过迭代自己的remove和add方法抛出异常 * 正确的翻译 * 抛出ConcurrentModificationException异常当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。 * 例如,某个线程在 Collection 上进行迭代时,通常不允许另一个线性修改该 Collection。 * 通常在这些情况下,迭代的结果是不明确的。 * 如果检测到这种行为,一些迭代器实现(包括 JRE 提供的所有通用 collection 实现)可能选择抛出此异常。 * 执行该操作的迭代器称为快速失败迭代器,因为迭代器很快就完全失败, * 而不会冒着在将来某个时间任意发生不确定行为的风险。 * 注意,此异常不会始终指出对象已经由不同线程并发修改。如果单线程发出违反对象协定的方法调用序列, * 则该对象可能抛出此异常。 * 例如,如果线程使用快速失败迭代器在 collection 上迭代时直接修改该 collection,则迭代器将抛出此异常。 * 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。 * 快速失败操作会尽最大努力抛出 ConcurrentModificationException。 * 因此,为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的做法, * 正确做法是:ConcurrentModificationException 应该仅用于检测 bug。 * the iterator will throw a {@link ConcurrentModificationException}. Thus, in * the face of concurrent modification, the iterator fails quickly and cleanly, * rather than risking arbitrary, non-deterministic behavior at an undetermined * time in the future.<p> * * Note that the fail-fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification. Fail-fast iterators * throw <tt>ConcurrentModificationException</tt> on a best-effort basis. * Therefore, it would be wrong to write a program that depended on this * exception for its correctness: <i>the fail-fast behavior of iterators * should be used only to detect bugs.</i><p> * * This class is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * * @author Josh Bloch * @author Neal Gafter * @version 1.56, 04/21/06 * @see Collection * @see List * @see LinkedList * @see Vector * @since 1.2 */
通过以上翻译得出如下总结:
1、可变得数组
2、允许添加所有元素包括null
3、不是同步的,Vecotor是线程同步
4、add操作的时间是根据添加对象的复杂度判断,而其他方法根据线性时间判断
5、添加许多对象可以用ensureCapacity,可以减少容量增加后的重新分配
6、此类线程不安全,可以 Collections.synchronizedList(list);
7、ConcurrentModificationException 应该仅用于检测 bug
相关文章推荐
- C#.Net ArrayList的使用方法
- VBS ArrayList Class vbs中的数组类
- C#中Arraylist的sort函数用法实例分析
- C#中ArrayList的使用方法
- C#中Array与ArrayList用法及转换的方法
- C#生成随机ArrayList的方法
- c# ArrayList的使用方法小总结
- php使用百度翻译api示例分享
- PHP实现C#山寨ArrayList的方法
- asp.net利用google的api做翻译
- PowerShell中使用ArrayList实现数组插入、删除、添加例子
- 在JavaScript中构建ArrayList示例代码
- js实现ArrayList功能附实例代码
- 2种Java删除ArrayList中的重复元素的方法
- java arrayList遍历的四种方法及Java中ArrayList类的用法
- Java ArrayList 数组之间相互转换
- java使用listIterator逆序arraylist示例分享
- Java中的Vector和ArrayList区别及比较
- Java中ArrayList类的使用方法
- java中ArrayList 、LinkList的区别分析