public interface CharacterIterator extends Cloneable
迭代器保持经常性指标,其有效范围是从getbeginindex()到getendindex();价值getendindex()包含允许零长度的文本范围的处理和历史原因。目前指数可以通过调用getindex()通过调用setindex(),first()直接提取,并last()。
方法previous()和next()用于迭代。他们的回报如果他们将从外面getbeginindex()到getendindex() - 1范围内,表明已达到的迭代器序列结束。所做的也是通过其他方法返回的,表明当前索引在这个范围之外。
实例:
遍历文本从开始到结束
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);
}
Modifier and Type | Field and Description |
---|---|
static char |
DONE
当迭代器已达到文本的结尾或开始时返回的常数。
|
Modifier and Type | Method and Description |
---|---|
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)
将位置设置为文本中指定的位置,并返回该字符。
|
static final char DONE
char first()
getBeginIndex()
char last()
getEndIndex()
char current()
getIndex()
char next()
char previous()
char setIndex(int position)
position
-在文本中的位置。有效值的范围从getbeginindex()到getendindex()。IllegalArgumentException是如果提供了一个无效的值被。
int getBeginIndex()
int getEndIndex()
int getIndex()
Object clone()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.