📌  相关文章
📜  Java中的 CollationElementIterator reset() 方法及示例

📅  最后修改于: 2022-05-13 01:54:39.417000             🧑  作者: Mango

Java中的 CollationElementIterator reset() 方法及示例

Java.text.CollationElementIterator类的reset()方法用于将迭代器的光标重置到输入字符串的开头。

句法:

public void reset()

参数:此方法不接受任何参数。
返回值:此方法没有任何返回值。

以下是说明reset()方法的示例:

示例 1:

Java
// Java program to demonstrate
// reset() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing testString
        String test = "abcd";
 
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
 
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
 
        // setting cursor of iterator
        cel.setOffset(2);
        cel.next();
 
        // display the result
        System.out.println(
            "current offset before calling reset() "
            + cel.getOffset());
 
        // reset the cursor to the
        // beginning of the string
        // using reset() method
        cel.reset();
 
        // display the result
        System.out.println(
            "\ncurrent offset after calling reset() "
            + cel.getOffset());
    }
}


Java
// Java program to demonstrate
// reset() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing
        // testString
        String test = "abcd";
 
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
 
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
 
        // setting cursor of iterator
        cel.setOffset(3);
        cel.previous();
 
        // display the result
        System.out.println(
            "current offset before calling reset() "
            + cel.getOffset());
 
        // reset the cursor to the beginning of the string
        // using reset() method
        cel.reset();
 
        // display the result
        System.out.println(
            "\ncurrent offset after calling reset() "
            + cel.getOffset());
    }
}


输出:
current offset before calling reset() 3

current offset after calling reset() 0

示例 2:

Java

// Java program to demonstrate
// reset() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing
        // testString
        String test = "abcd";
 
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
 
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
 
        // setting cursor of iterator
        cel.setOffset(3);
        cel.previous();
 
        // display the result
        System.out.println(
            "current offset before calling reset() "
            + cel.getOffset());
 
        // reset the cursor to the beginning of the string
        // using reset() method
        cel.reset();
 
        // display the result
        System.out.println(
            "\ncurrent offset after calling reset() "
            + cel.getOffset());
    }
}
输出:
current offset before calling reset() 2

current offset after calling reset() 0

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/CollationElementIterator.html#reset–