当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CollationElementIterator setOffset()用法及代码示例


java.text.Collat​​ionElementIterator类的setOffset()方法用于将迭代器的光标设置为作为参数传递的特定索引。

用法:

public void setOffset(int newOffset)

参数:此方法采用整数值newOffset,此时必须设置光标。


返回值:此方法无返回值。

下面是说明setOffset()方法的示例:

范例1:

// Java program to demonstrate 
// setOffset() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        // creating and intiallizing testString 
        String test = "Code Geeks 123"; 
  
        // creating and intiallizing 
        // RuleBasedCollator object 
        RuleBasedCollator rbc 
            = (RuleBasedCollator)(Collator.getInstance()); 
  
        // creating and intiallizing 
        // CollationElementIterator 
        CollationElementIterator cel 
            = rbc.getCollationElementIterator(test); 
  
        // setting offset to index 4 
        // using setOffset() method 
        cel.setOffset(4); 
  
        // display the result 
        System.out.println("current offset is "
                           + cel.getOffset()); 
    } 
}
输出:
current offset is 4

范例2:

// Java program to demonstrate 
// setOffset() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        // creating and intiallizing testString 
        String test = "GeeksForGeeks"; 
  
        // creating and intiallizing 
        // RuleBasedCollator object 
        RuleBasedCollator rbc 
            = (RuleBasedCollator)(Collator.getInstance()); 
  
        // creating and intiallizing 
        // CollationElementIterator 
        CollationElementIterator cel 
            = rbc.getCollationElementIterator(test); 
  
        // after call of setOffset() method 
        // all next() method will become redundent 
        cel.next(); 
        cel.next(); 
        cel.next(); 
        cel.next(); 
  
        // setting cursor of iterator to index 0 
        // using setOffset() method 
        cel.setOffset(0); 
  
        // display the result 
        System.out.println("current offset is "
                           + cel.getOffset()); 
    } 
}
输出:
current offset is 0

参考: https://docs.oracle.com/javase/9/docs/api/java/text/CollationElementIterator.html#setOffset-int-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 CollationElementIterator setOffset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。