RandomAccess是一个声明接口,主要用来区分对List不同实例的访问策略。比如ArrayList是顺序访问的策略最快,也就是RandomAccess随机访问;LinkedList是链表实现,使用迭代器一个个的访问,不去实现RandomAccess接口,表示它不支持随机访问(下标位置)。具体内容看下面jdk的描述。在使用List的接口的时候最好可以根据RandomAccess来判断实例的访问策略,加快访问list的性能。
举例代码如下:
public static <T> List<T> unmodifiableList(List<? extends T> list) { return (list instanceof RandomAccess ? new UnmodifiableRandomAccessList<>(list) : new UnmodifiableList<>(list)); }
jdk的原文描述 写道
Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.
The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.
It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
This interface is a member of the Java Collections Framework.
Since:
1.4
The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.
It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
This interface is a member of the Java Collections Framework.
Since:
1.4
相关推荐
It also covers most of the classes in the main packages (java.lang, java.util, java.io) as implemented in the J2SE™ Development Kit 5.0 (more commonly known as JDK 5.0, or in the older nomenclature ...
#002 implements List, RandomAccess, #003 Cloneable, java.io.Serializable #004 { #005 private transient E[] elementData; #006 private int size; #007 public ArrayList(int initialCapacity) { #008 ...
java 1.8 源码 jdk1.8.0_151-源码的中文翻译和一些自己的理解 声明 作者现在大四快要毕业,在实习中,为了在未来成为一名架构师,下定决心开始读Java的...RandomAccess.java ArrayList.java LinkedList.java Vector.jav
The Nimbus Look and Feel has been moved from the com.sun.java.swing package to the javax.swing package; see the javax.swing.plaf.nimbus package. Mixing Heavyweight and Lightweight Components is ...
7. **随机数生成**:`java.util.Random` 类适用于一般的随机数生成需求,如验证码生成。在挑战算法或高安全性的随机数需求时,可能需要使用更专业的随机数生成库。正确选项为 B(验证码的随机数生成)。 8. **...
addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard Edition (J2SE) 1.2.x or 1.3.x. A general familiarity with object-oriented programming ...
3. **JDK (Java Development Kit)**:Java开发工具包,包含了编写、编译和运行Java程序所需的所有工具。 4. **JVM (Java Virtual Machine)**:Java虚拟机,负责解释和执行Java字节码。 5. **Compile**:编译,将源...
5. **JDK**: Java Development Kit,Java开发工具包 - 用于编写、测试和调试Java程序的一套软件工具。 6. **JVM**: Java Virtual Machine,Java虚拟机 - 解释和执行Java字节码的平台独立环境。 7. **Compile**: ...
3. **JDK (Java Development Kit)**:Java开发工具包,包含了编译、运行Java程序所需的工具和库。 4. **JVM (Java Virtual Machine)**:Java虚拟机,它是Java程序的运行环境,负责解释和执行字节码。 5. **Compile...
18. Date、Calendar和DateFormat类都是Java中处理日期和时间的类,位于`java.util`包内。 19. TCP是面向连接的协议,需要明确客户端和服务器端;它提供可靠无差错的数据传输。UDP是无连接的,资源消耗小,通信效率...
11. 集合与数组工具类:Java的`java.util`包中提供了`Collections`类用于操作集合,`Arrays`类用于操作数组。 12. 数组创建:正确创建数组的Java代码是`int[] myArray={1,2,3,4,5};`(C)。 13. 数据库模型历史:最...
3. **JDK**: Java Development Kit(JAVA开发工具包)— 是JAVA的官方开发环境,包含了JAVA运行环境(JRE)、JAVA工具和JAVA基础类库等。 4. **JVM**: Java Virtual Machine(JAVA虚拟机)— 是一个可以执行JAVA字节...
3. **JDK (Java Development Kit)**:Java开发工具包,包含了编写、编译、调试Java程序所需的所有工具,包括JRE(Java Runtime Environment)和开发工具。 4. **JVM (Java Virtual Machine)**:Java虚拟机,执行...
在Java中,这个过程通常是指将.java文件编译成.class文件。 #### Run 运行,指的是执行程序的过程。在Java中,经过编译后的字节码由JVM解释执行。 #### Class 类,是Java编程的基本单元之一,用来定义对象的属性和...
implements List, RandomAccess, Cloneable, java.io.Serializable ``` 数组的初始容量默认为 10: ```java private static final int DEFAULT_CAPACITY = 10; ``` #### 扩容机制 当向 `ArrayList` 添加元素时...
* JDK: Java 开发工具包 * Compile: 编译 * System: 系统 * Variable: 变量 * Method: 方法 * Get: 得到 * Default: 默 认 * Print: 打印 * Line: 行 * Operation: 操作 * Array: 数组 * Member-variable: 成员变量 ...