Java の Iterator インターフェイスと LIstIterator インターフェイスの詳細な紹介
この記事では主に Java IteratorインターフェースとLIstIteratorインターフェース解析の関連情報を紹介しますので、必要な方は
Java IteratorインターフェースとLIstIteratorインターフェース解析
目次
を参照してください。1 .Iterator インターフェース
2. ListIterator
3. Iterator と ListIterator の違い
テキスト
ArrayList のソースコードを見続ける前に、まず Iterator インターフェースと ListIterator インターフェースについて理解します。 ArrayList がそれらをどのように実装するかを詳しく説明します。
インターフェースが単なる仕様であることはわかっています。 がインターフェースを継承してそのメソッドを実装する場合、インターフェースのメソッドの説明に従わなければなりません。
1.Iterator インターフェイス
フレームワーク の Enumeratrion を置き換えます。イテレータは主に 2 つの点で列挙型と異なります。
イテレータを使用すると、呼び出し元は反復プロセス中にコレクションから要素を削除できます。 メソッド名が改良されました。Iterator のソース コードは次のとおりです:
/** * An iterator over a collection. {@code Iterator} takes the place of * {@link Enumeration} in the Java Collections Framework. Iterators * differ from enumerations in two ways: * Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. * Method names have been improved. * This interface is a member of the Java Collections Framework. * @param <E> the type of elements returned by this iterator*/ public interface Iterator<E> { /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * @return {@code true} if the iteration has more elements */ boolean hasNext(); /** * Returns the next element in the iteration. * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ E next(); /** * Removes from the underlying collection the last element returned * by this iterator (optional operation). This method can be called * only once per call to {@link #next}. The behavior of an iterator * is unspecified if the underlying collection is modified while the * iteration is in progress in any way other than by calling this * method. * * @implSpec * The default implementation throws an instance of * {@link UnsupportedOperationException} and performs no other action. * * @throws UnsupportedOperationException if the {@code remove} * operation is not supported by this iterator * * @throws IllegalStateException if the {@code next} method has not * yet been called, or the {@code remove} method has already * been called after the last call to the {@code next} * method */ default void remove() { throw new UnsupportedOperationException("remove"); } /** * Performs the given action for each remaining element until all elements * have been processed or the action throws an exception. Actions are * performed in the order of iteration, if that order is specified. * Exceptions thrown by the action are relayed to the caller. * * @implSpec * <p>The default implementation behaves as if: * <pre class="brush:php;toolbar:false">{@code * while (hasNext()) * action.accept(next()); * }* * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEachRemaining(Consumer super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
chR
emaining メソッドが追加されています。 さらに詳しい手順については、ソースコードのコメントをご覧ください。
ListIterator は、Iterator に基づいた add、set、
prev
/** * An iterator for lists that allows the programmer * to traverse the list in either direction, modify * the list during iteration, and obtain the iterator's * current position in the list. A {@code ListIterator} * has no current element; its <I>cursor position</I> always * lies between the element that would be returned by a call * to {@code previous()} and the element that would be * returned by a call to {@code next()}. * An iterator for a list of length {@code n} has {@code n+1} possible * cursor positions, as illustrated by the carets ({@code ^}) below: * <PRE> * Element(0) Element(1) Element(2) ... Element(n-1) * cursor positions: ^ ^ ^ ^ ^ ** Note that the {@link #remove} and {@link #set(Object)} methods are * not defined in terms of the cursor position; they are defined to * operate on the last element returned by a call to {@link #next} or * {@link #previous()}. * * This interface is a member of the Java Collections Framework.*/ public interface ListIterator
1).hasNext() 前方にトラバースする場合、次の要素がある場合は true を返します。 2).next( ) 次の要素の値を返し、ポインタに 1 を加算します。
3).hasPrevious() 逆方向にトラバースする場合、まだ要素がある場合は true を返します。 previous() は前の要素の値を返し、ポインタを 1 つ進めます 5).nextIndex() は、この時点で next() メソッドが呼び出されたときに返される要素のindex を返します。 ).previousIndex() は、previous() メソッドがこの時点で呼び出されたときに返されるものを返します 要素のインデックス
7).remove() は、最新の next() またはprevious() の呼び出しによって返された要素を削除します。 ) メソッド (オプション);
8).set(E e) 要素 e を使用して if this を呼び出して返された要素を置き換えます
9).add(E e) を追加します。この時点で next() を呼び出して返された要素の前の要素、またはこの時点で next() を呼び出して返された要素の後の要素。
さらに詳しい手順については、ソースコード内のコメントを読んでください。
3. Iterator と ListIterator の違い 次の表で Iterator と ListIterator のメソッドを比較します。
は次( )
次があります ()
オーバーレイ
next() | オーバーレイremove() | |
オーバーレイ |
forEachRemaining (Consumer super E> action) | |
inherit |
hasPrevious() | |
前() | ||
nextIndex() | ||
previousIndex() | ||
set(E e) | ||
add(E e) | ||
2 つの違い 主な機能は次のとおりです: | 1) Iterator は 1 つの方向にのみ移動できますが、ListIterator は 2 つの方向に移動できます。 Iterator は要素の削除のみ可能です; | |
以上がJava の Iterator インターフェイスと LIstIterator インターフェイスの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の乱数ジェネレーターのガイド。ここでは、Java の関数について例を挙げて説明し、2 つの異なるジェネレーターについて例を挙げて説明します。

Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。
