JavaTM 2 Platform
Standard Ed. 6

java.text
接口 CharacterIterator

所有超级接口:
Cloneable
所有已知子接口:
AttributedCharacterIterator
所有已知实现类:
Segment, StringCharacterIterator

public interface CharacterIterator
extends Cloneable

此接口定义了对文本进行双向迭代的协议。迭代器对有界字符序列进行迭代。这些字符使用从 getBeginIndex() 返回的值开始,一直到 getEndIndex()-1 返回的值结束之间的值进行索引。

迭代器维护当前的字符索引,索引值的有效范围是从 getBeginIndex() 到 getEndIndex();出于历史原因,包括了值 getEndIndex() 以允许处理零长度的文本范围。可以通过调用 getIndex() 获取当前索引,还可以通过调用 setIndex()、first() 和 last() 直接设置当前索引。

使用方法 previous() 和 next() 进行迭代。如果方法进行到从 getBeginIndex() 到 getEndIndex() -1 的范围之外,则返回 DONE,指示迭代器已经到达序列末尾。DONE 还可以由其他方法返回,以指示当前索引超出了此范围。

示例:

从前往后遍历文本

 public void traverseForward(CharacterIterator iter) {
     for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
         processChar(c);
     }
 }
 
从后往前逆向遍历文本
 public void traverseBackward(CharacterIterator iter) {
     for(char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
         processChar(c);
     }
 }
 
从文本中给定的位置进行正向和逆向遍历。在此例中调用 notBoundary() 表示某个附加的停止遍历的标准。
 public void traverseOut(CharacterIterator iter, int pos) {
     for (char c = iter.setIndex(pos);
              c != CharacterIterator.DONE && notBoundary(c);
              c = iter.next()) {
     }
     int end = iter.getIndex();
     for (char c = iter.setIndex(pos);
             c != CharacterIterator.DONE && notBoundary(c);
             c = iter.previous()) {
     }
     int start = iter.getIndex();
     processSection(start, end);
 }
 

另请参见:
StringCharacterIterator, AttributedCharacterIterator

字段摘要
static char DONE
          当迭代器已到达文本末尾或开始处时返回的常量。
 
方法摘要
 Object clone()
          创建此迭代器的一个副本
 char current()
          获取当前位置(由 getIndex() 返回)的字符。
 char first()
          将位置设置为 getBeginIndex(),并返回该位置的字符。
 int getBeginIndex()
          返回文本的起始索引。
 int getEndIndex()
          返回文本的结束索引。
 int getIndex()
          返回当前索引。
 char last()
          将位置设置为 getEndIndex()-1(如果文本为空,则为 getEndIndex()),并返回该位置的字符。
 char next()
          将迭代器的索引加一,并返回新索引处的字符。
 char previous()
          将迭代器的索引减一,并返回新索引处的字符。
 char setIndex(int position)
          将位置设置为文本中的指定位置,并返回该字符。
 

字段详细信息

DONE

static final char DONE
当迭代器已到达文本末尾或开始处时返回的常量。其值为 '\\uFFFF',即不应该出现在任何有效 Unicode 字符串中的 "not a character" 值。

另请参见:
常量字段值
方法详细信息

first

char first()
将位置设置为 getBeginIndex(),并返回该位置的字符。

返回:
文本中的第一个字符,如果文本为空,则返回 DONE
另请参见:
getBeginIndex()

last

char last()
将位置设置为 getEndIndex()-1(如果文本为空,则为 getEndIndex()),并返回该位置的字符。

返回:
文本中的最后一个字符,如果文本为空,则返回 DONE
另请参见:
getEndIndex()

current

char current()
获取当前位置(由 getIndex() 返回)的字符。

返回:
当前位置的字符;如果当前位置已超出文本末尾,则返回 DONE。
另请参见:
getIndex()

next

char next()
将迭代器的索引加一,并返回新索引处的字符。如果得到的索引大于等于 getEndIndex(),则将当前索引重置为 getEndIndex(),并返回值 DONE。

返回:
新位置的索引;如果该新位置已超出文本范围的末尾,则返回 DONE。

previous

char previous()
将迭代器的索引减一,并返回新索引处的字符。如果当前索引为 getBeginIndex(),则新索引仍为 getBeginIndex(),并返回值 DONE。

返回:
新位置的字符;如果当前位置等于 getBeginIndex(),则返回 DONE。

setIndex

char setIndex(int position)
将位置设置为文本中的指定位置,并返回该字符。

参数:
position - 文本中的位置。从 getBeginIndex() 到 getEndIndex() 的有效值。如果提供了无效值,则抛出 IllegalArgumentException。
返回:
指定位置的字符;如果指定位置等于 getEndIndex(),则返回 DONE

getBeginIndex

int getBeginIndex()
返回文本的起始索引。

返回:
文本开始处的索引。

getEndIndex

int getEndIndex()
返回文本的结束索引。此索引是文本末尾后面第一个字符的索引。

返回:
文本中最后一个字符后面的索引

getIndex

int getIndex()
返回当前索引。

返回:
当前索引。

clone

Object clone()
创建此迭代器的一个副本

返回:
迭代器的副本

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策