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

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

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

Java.text.CollationElementIterator 类getOffset()方法用于获取 CollationElementIterator 当前指向的整理器中存在的项目的索引。

句法:

public int getOffset()

参数:此方法不接受任何参数。

返回值:该方法返回迭代器当前指向的元素的偏移量

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

示例 1:

// Java program to demonstrate getOffset() 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 = "Code Geeks 123";
  
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
  
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
  
        // setting offset to index 4
        cel.setOffset(4);
  
        // getting offset of cel
        // using getOffset() method
        int value = cel.getOffset();
  
        // display the result
        System.out.println("current offset is "
                           + value);
    }
}
输出:
current offset is 4

示例 2:

// Java program to demonstrate getOffset() 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 = "Code Geeks 123";
  
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
  
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
  
        // setting offset to index 4
        cel.setOffset(4);
  
        // calling next element
        cel.next();
  
        // getting offset of cel
        // using getOffset() method
        int value = cel.getOffset();
  
        // display the result
        System.out.println("current offset is "
                           + value);
    }
}
输出:
current offset is 5

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