java 集合 迭代器_使用迭代器访问Java集合

java 集合 迭代器

To access elements of a collection, either we can use index if collection is list based or we need to traverse the element. There are three possible ways to traverse through the elements of any collection.

要访问集合的元素,或者如果集合基于列表,我们可以使用index,或者我们需要遍历该元素。 有三种可能的方式遍历任何集合的元素。

  1. Using Iterator interface

    使用迭代器界面

  2. Using ListIterator interface

    使用ListIterator界面

  3. Using for-each loop

    使用for-each循环

使用迭代器访问元素 (Accessing elements using Iterator)

Iterator is an interface that is used to iterate the collection elements. It is part of java collection framework. It provides some methods that are used to check and access elements of a collection.

迭代器是用于迭代集合元素的接口。 它是Java收集框架的一部分。 它提供了一些用于检查和访问集合元素的方法。

Iterator Interface is used to traverse a list in forward direction, enabling you to remove or modify the elements of the collection. Each collection classes provide iterator() method to return an iterator.

迭代器接口用于向前遍历列表,使您可以删除或修改集合的元素。 每个集合类都提供iterator()方法来返回迭代器。

迭代器接口方法 (Iterator Interface Methods)

MethodDescription
boolean hasNext()Returns true if there are more elements in the collection. Otherwise, returns false.
E next()Returns the next element present in the collection. Throws NoSuchElementException if there is not a next element.
void remove()Removes the current element. Throws IllegalStateException if an attempt is made to call remove() method that is not preceded by a call to next() method.
方法 描述
布尔hasNext() 如果集合中有更多元素,则返回true 。 否则,返回false。
E next() 返回集合中存在下一个元素 。 如果没有下一个元素,则抛出NoSuchElementException。
无效remove() 删除当前元素。 如果尝试调用remove()方法但未调用next()方法,则抛出IllegalStateException。

迭代器示例 (Iterator Example)

In this example, we are using iterator() method of collection interface that returns an instance of Iterator interface. After that we are using hasNext() method that returns true of collection contains an elements and within the loop, obtain each element by calling next() method.

在此示例中,我们使用集合接口的iterator()方法,该方法返回Iterator接口的实例。 之后,我们使用hasNext()方法,该方法返回的true包含集合中的一个元素,并在循环内,通过调用next()方法获取每个元素。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {
    ArrayList< String> ar = new ArrayList< String>();
    ar.add("ab");
    ar.add("bc");
    ar.add("cd");
    ar.add("de");
    Iterator it = ar.iterator();    //Declaring Iterator
    while(it.hasNext())
    {  
      System.out.print(it.next()+" ");
    }
  }
}

ab bc cd de

ab bc cd de

使用ListIterator访问元素 (Accessing elements using ListIterator)

ListIterator Interface is used to traverse a list in both forward and backward direction. It is available to only those collections that implements the List Interface.

ListIterator接口用于向前向后遍历列表。 它仅对实现列表接口的那些集合可用。

ListIterator的方法: (Methods of ListIterator:)

MethodDescription
void add(E obj) Inserts obj into the list in front of the element that will be returned by the next call to next() method.
boolean hasNext()Returns true if there is a next element. Otherwise, returns false.
boolean hasPrevious()Returns true if there is a previous element. Otherwise, returns false.
E next()Returns the next element. A NoSuchElementException is thrown if there is not a next element.
int nextIndex()Returns the index of the next element. If there is not a next element, returns the size of the list.
E previous()Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
int previousIndex() Returns the index of the previous element. If there is not a previous element, returns -1.
void remove()Removes the current element from the list. An IllegalStateException is thrown if remove() method is called before next() or previous() method is invoked.
void set(E obj) Assigns obj to the current element. This is the element last returned by a call to either next() or previous() method.
方法 描述
无效add(E obj) 将obj插入到元素的列表中,该元素将在下次调用next()方法时返回。
布尔hasNext() 如果存在下一个元素,则返回true。 否则,返回false。
布尔hasPrevious() 如果存在上一个元素,则返回true。 否则,返回false。
E next() 返回下一个元素。 如果没有下一个元素,则抛出NoSuchElementException。
int nextIndex() 返回下一个元素的索引。 如果没有下一个元素,则返回列表的大小。
E previous() 返回上一个元素。 如果没有上一个元素,则抛出NoSuchElementException。
int previousIndex() 返回上一个元素的索引。 如果没有前一个元素,则返回-1。
无效remove() 从列表中删除当前元素。 如果在调用next()或previous()方法之前调用remove()方法,则抛出IllegalStateException。
无效set(E obj) 将obj分配给当前元素。 这是调用next()或previous()方法最后返回的元素。

ListIterator示例 (ListIterator Example)

Lets create an example to traverse the elements of ArrayList. ListIterator works only with list collection.

让我们创建一个示例来遍历ArrayList的元素。 ListIterator仅适用于列表集合。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {
    ArrayList< String> ar = new ArrayList< String>();
    ar.add("ab");
    ar.add("bc");
    ar.add("cd");
    ar.add("de");
    ListIterator litr = ar.listIterator();
    while(litr.hasNext())   //In forward direction
    {
      System.out.print(litr.next()+" ");
    }
    while(litr.hasPrevious())   //In backward direction
    {
      System.out.print(litr.previous()+" ");
    }
  }
}

ab bc cd de de cd bc ab

ab bc cd de de cd bc ab

每个循环 (for-each loop)

for-each version of for loop can also be used for traversing the elements of a collection. But this can only be used if we don't want to modify the contents of a collection and we don't want any reverse access. for-each loop can cycle through any collection of object that implements Iterable interface.

for-each版本for环也可以用于遍历集合中的元素。 但这仅在我们不想修改集合的内容并且我们不希望有任何反向访问的情况下使用。 for-each循环可以在实现Iterable接口的任何对象集合之间循环。

范例: (Exmaple:)

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {
    LinkedList< String> ls = new LinkedList< String>();
    ls.add("a");
    ls.add("b");
    ls.add("c");
    ls.add("d");
    for(String str : ls)
    {
      System.out.print(str+" ");
    }
  }
}

a b c d

A B C D

使用for循环遍历 (Traversing using for loop)

we can use for loop to traverse the collection elements but only index-based collection can be accessed. For example, list is index-based collection that allows to access its elements using the index value.

我们可以使用for循环遍历collection元素,但是只能访问基于索引的collection。 例如,list是基于索引的集合,允许使用索引值访问其元素。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {
    LinkedList<String> ls = new LinkedList<String>();
    ls.add("a");
    ls.add("b");
    ls.add("c");
    ls.add("d");
    for(int i = 0; i<ls.size(); i++)
    {
      System.out.print(ls.get(i)+" ");
    }
  }
}

a b c d

A B C D

翻译自: https://www.studytonight.com/java/iterator-in-collection.php

java 集合 迭代器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值