Abstract Interface CharacterIterator in java

digitization, transformation, binary

Abstract Interface CharacterIterator in java

CharacterIterator interface defines an API for portably iterating through the characters that comprise a string of text, regardless of the encoding of that text. Such an API is necessary because the number of bytes per character is different for different encodings, and some encodings even use variable-width characters within the same string of text.

In addition to allowing iteration, a class that implements the CharacterIterator interface for non-Unicode text also performs translation of characters from their native encoding to standard Java Unicode characters.

Ads code goes here

CharacterIterator is similar to java.util.Enumeration, but is somewhat more complex than that interface. The first() and last() methods return the first and last characters in the text, and the next() and prev() methods allow you to loop forward or backward through the characters of the text. These methods return the DONE constant when they go beyond the first or last character in the text–a test for this constant can be used to terminate a loop.

The CharacterIterator interface also allows random access to the characters in a string of text.The getBeginIndex() and getEndIndex() methods return the character positions for the start and end of the string, and setIndex() sets the current position. getIndex() returns the index of the current position and current() returns the character at that position.

The CharacterIterator interface defines methods that support bidirectional movement through a sequence of text. The interface is implemented by classes that maintain a current position in a sequence of characters. The interface provides methods for moving to the first, last, next, and previous characters in the sequence. The BreakIterator classes uses this interface to locate boundaries in textual sequences.

READ  Class Stack in Java

The structure of interface CharacterIterator is given as

public abstract interface java.text.CharacterIterator extends java.lang.Cloneable {
// Constants
public static final char DONE;
// Public Instance Methods
public abstract Object clone(); // Overrides Object
public abstract char current();
public abstract char first();
public abstract int getBeginIndex();
public abstract int getEndIndex();
public abstract int getIndex();
public abstract char last();
public abstract char next();
public abstract char previous();
public abstract char setIndex(int position);
}

The details of the class structure are given as follows:

public static final char DONE;

public static final char DONE is a constant that indicates that the beginning or end of the text has been reached. It can be returned by next() or previous().

public abstract Object clone();

public abstract Object clone() method creates a copy of this CharacterIterator and returns it.

This method returns a copy of this CharacterIterator.

public abstract char current();

public abstract char current() method returns the character at the current position of this CharacterIterator. The current position is returned by getIndex().

This method returns the character at the current position of this CharacterIterator or DONE if the current position is not within the text sequence.

public abstract char first();

public abstract char first() method returns the character at the first position in this CharacterIterator. The first position is returned by getBeginIndex(). The current position of the iterator is set to this position.

This method returns the first character in this CharacterIterator.

public abstract int getBeginIndex();

public abstract int getBeginIndex() method returns the index of the beginning of the text for this CharacterIterator.

This method returns the index of the first character in this CharacterIterator.

public abstract int getEndIndex();

public abstract int getEndIndex() method returns the index of the character following the end of the text for this CharacterIterator.

READ  Class EOFException in Java

This method returns the index after the last character in this CharacterIterator.

public abstract int getIndex();

public abstract int getIndex() method returns the current position, or index, of this CharacterIterator.

This method returns the index of the current character in this CharacterIterator.

public abstract char last();

public abstract char last() method returns the character at the ending position of this CharacterIterator. The last position is the value of getEndIndex()-1. The current position of the iterator is set to this position.

This method returns the last character in this CharacterIterator.

public abstract char next();

public abstract char next() method increments the current position of this CharacterIterator by one and returns the character at the new position. If the current position is already at getEndIndex(), the position is not changed, and DONE is returned.

This method returns the next character in this CharacterIterator or DONE if the current position is already at the end of the text.

public abstract char previous();

public abstract char previous() method decrements the current position of this CharacterIterator by one and returns the character at the new position. If the current position is already at getBeginIndex(), the position is not changed, and DONE is returned.

This method returns the previous character in this CharacterIterator or DONE if the current position is already at the beginning of the text.

public abstract char setIndex(int position);

public abstract char setIndex(int position) method sets the current position, or index, of this CharacterIterator to the given position.

This method returns the character at the specified position in this CharacterIterator.

READ  Class StreamCorruptedException in java

Parameter
position – The new position.

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *