>  기사  >  Java  >  Java의 RandomAccess 인터페이스 소스 코드에 대한 자세한 분석

Java의 RandomAccess 인터페이스 소스 코드에 대한 자세한 분석

黄舟
黄舟원래의
2017-05-28 09:06:102022검색

이 글에서는 주로 Java의 RandomAccess인터페이스소스 코드 분석 관련 정보를 소개합니다. 도움이 필요한 친구는

Java의 RandomAccess 인터페이스 소스 코드 분석

을 참조할 수 있습니다. RandomAccess는 java.util 패키지에 있는 인터페이스입니다.

이 인터페이스의 기능댓글은 매우 명확하게 작성되었습니다.

/**
 * Marker interface used by <tt>List</tt> 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.
 * List实现所使用的标记接口,用来表明实现了这些接口的list支持快速(通常是常数时间)随机访问。
 * 这个接口的主要目的是允许一般的算法更改它们的行为,以便在随机或者顺序存取列表时能提供更好的性能。
 * <p>The best algorithms for manipulating random access lists (such as
 * <tt>ArrayList</tt>) can produce quadratic behavior when applied to
 * sequential access lists (such as <tt>LinkedList</tt>). Generic list
 * algorithms are encouraged to check whether the given list is an
 * <tt>instanceof</tt> 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.
 * 操作随机访问列表(如ArrayList)的最佳算法在应用于顺序存取列表时,有可能产生二次项行为。
 * 泛型算法列表鼓励在将某个算法应用于顺序存取列表可能导致差的性能之前,先检查给定的列表是否是这个接口的一个实例,
 * 并在需要时去改变这些算法的行为以保证性能。
 * <p>It is recognized that the distinction between random and sequential
 * access is often fuzzy. For example, some <tt>List</tt> implementations
 * provide asymptotically linear access times if they get huge, but constant
 * access times in practice. Such a <tt>List</tt> implementation
 * should generally implement this interface. As a rule of thumb, a
 * <tt>List</tt> implementation should implement this interface if,
 * for typical instances of the class, this loop:

 * 随机访问和顺序存取之间的界限通常是模糊的。例如,一些List实现在变得很大时会导致渐进的非线性访问时间,但实际上是常量访问时间。
 * 这样的List实现通常都应该实现该接口。
 * 一般来说,某个List实现如果(对某些典型的类的实例来说)满足下面的条件,就应该实现这个接口:循环
 * <pre class="brush:php;toolbar:false">
 *   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. * 这个接口是Java集合框架的一员。 * @since 1.4 */ public interface RandomAccess { }

RandomAccess는 빈 인터페이스이고 빈 인터페이스의 기능은 일반적으로 식별자 역할을 합니다.

간단히 말하면 목록이 RandomAcess 인터페이스를 구현하는지 확인하는 것입니다. 그렇다면 더 빠른 액세스를 위해 아래에 표시된 간단한 for 루프를 사용하세요.

for (int i=0, n=list.size(); i < n; i++)
   list.get(i);

RandomAcess 인터페이스가 구현되지 않은 경우 다음 반복자 루프 액세스 속도는 상대적으로 빠릅니다.

for (Iterator i=list.iterator(); i.hasNext(); )
   i.next();

instanceof를 사용하여 판단하세요. 즉

 if (list instanceof RandomAccess)
입니다.

위 내용은 Java의 RandomAccess 인터페이스 소스 코드에 대한 자세한 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.