當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java CharacterIterator setIndex()用法及代碼示例


Java中java.text.CharacterIterator接口的setIndex()方法用於設置由CharacterIterator讀取的當前索引。此方法將要設置的索引作為參數,並在該索引處設置此CharacterIterator的索引,然後返回該字符。

用法:

public char setIndex(int index)

參數:此方法將要設置的索引作為參數,並設置此CharacterIterator的索引。


返回值:此方法返回此方法設置的索引處的字符。

異常:如果指定的索引不在有效範圍內(getBeginIndex(),getEndIndex() – 1),則此方法引發IllegalArgumentException。

程序:

// Java program to demonstrate 
// the above method 
  
import java.text.*; 
import java.util.*; 
  
public class CharacterIteratorDemo { 
    public static void main(String[] args) 
    { 
  
        CharacterIterator characterIterator 
            = new StringCharacterIterator( 
                "GeeksForGeeks"); 
  
        System.out.println("Current Index: "
                           + characterIterator 
                                 .getIndex()); 
  
        int index = 5; 
  
        System.out.println("Updated the index to : "
                           + index); 
  
        System.out.println("Character at new"
                           + " current index: "
                           + characterIterator 
                                 .setIndex(index)); 
    } 
}
輸出:
Current Index: 0
Updated the index to : 5
Character at new current index: F

參考: https://docs.oracle.com/javase/9/docs/api/java/text/CharacterIterator.html#setIndex-int-



相關用法


注:本文由純淨天空篩選整理自ShubhamMaurya3大神的英文原創作品 CharacterIterator setIndex() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。