当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)

接口 java.text.CharacterIterator

public interface CharacterIterator
extends Cloneable

该接口为文本的双向迭代定义协议。迭代器在有边界的字符序列上迭代。字符以 getBeginIndex 返回的值作为开始索引和继续用 getEndIndex()-1 的值生成索引。当前字符的索引可通过调用 getIndex 来获取。调用 setIndex 将引起在字符序列中把迭代器移到新位置。如果在任何时间迭代器的当前索引移到 getBeginIndex 和 getEndIndexIf 的范围之外,则 previous() 和 next() 将返回 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.prev()) {
         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.prev()) {}
     int start = iter.getIndex();
 processSection(start,end);
 }
 

参见:
StringCharacterIterator

变量索引

DONE
当迭代器已到达文本结尾或文本开头时返回的常量。

方法索引

clone()
创建该迭代器的一个拷贝
current()
获得当前位置的字符 (就象由 getIndex() 返回的字符一样)。
first()
将位置设置为 getBeginIndex() 并返回该位置的字符。
getBeginIndex()
返回文本的开始位置索引。
getEndIndex()
返回文本的结束位置索引。
getIndex()
返回当前位置的索引。
last()
将位置设置为 getEndIndex()-1, 并返回在该位置的字符。
next()
将迭代器的索引加 1 并返回新索引处的字符。
previous()
将迭代器的索引减 1 并返回新索引处的字符。
setIndex(int)
将位置设置为文本中指定的位置并返回那个字符。

变量

DONE
 public static final char DONE
当迭代器已到达结尾或文本开头时返回的常量。unicode 2.0 标准状态 '\\uFFFF' 是一个无效的 unicode 值,它不能出现在任何有效的 unicode 字符串中。


方法

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

返回值:
文本中的第一个字符
参见:
getBeginIndex
last
 public abstract char last()
将位置设置为 getEndIndex()-1, 并返回在该位置的字符。

返回值:
文本中的最后一个字符
参见:
getEndIndex
current
 public abstract char current()
获得当前位置的字符(就象由 getIndex() 返回的字符)。

返回值:
返回当前位置的字符,如果当前位置超出了文本的末尾则返回 DONE 。
参见:
getIndex
next
 public abstract char next()
将迭代器的索引加 1 并返回新索引处的字符。如果结果索引大于或等于 getEndIndex(),当前的索引设为 getEndIndex() 并返回 DONE。

返回值:
返回新位置的字符,如果当前位置超出了文本的末尾则返回 DONE 。
previous
 public abstract char previous()
将迭代器的索引减 1 并返回新索引处的字符。如果结果索引小于或等于 getBeginIndex(),将当前的索引设为 getBeginIndex() 并返回 DONE。

返回值:
返回新位置的字符,如果当前位置超出了文本的末尾则返回 DONE 。
setIndex
 public abstract char setIndex(int position)
将位置设置为文本中指定的位置并返回那个字符。

参数:
position - 文本内的位置。从 getBeginIndex() 到 getEndIndex() - 1 范围内的有效值。如果提供一个无效值,则抛出 IllegalArgumentException 。
返回值:
在指定位置的字符。
getBeginIndex
 public abstract int getBeginIndex()
返回文本开始位置的索引。

返回值:
文本开始位置的索引。
getEndIndex
 public abstract int getEndIndex()
返回文本结束位置的索引。索引是紧跟文本末尾后第一个字符的索引。

返回值:
文本结束位置的索引。
getIndex
 public abstract int getIndex()
返回当前位置的索引。

返回值:
当前位置的索引。
clone
 public abstract Object clone()
创建该迭代器的一个拷贝

返回值:
该迭代器的一个拷贝
覆盖:
Object 中的 clone

当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)