Java CharBuffer charAt()方法及实例

Java CharBuffer charAt()方法及实例

java.nio.CharBuffer 类的 charAt() 方法用于读取相对于当前位置的给定索引处的字符。

语法

public final char charAt(int index)

参数: 该方法接收要读取的字符的索引,相对于位置而言;必须是非负数,并且小于remain()。

返回值 :该方法返回index position() + index处的字符。

异常: 如果索引的前提条件不成立,该方法会抛出 IndexOutOfBoundsException

下面是说明charAt(int index)方法的例子。

例1 :

// Java program to demonstrate
// charAt() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating the CharBuffer
        try {
  
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(3);
  
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                .rewind();
  
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
  
            // Read char at particular Index
            // using charAt() method
            char value = charbuffer.charAt(2);
  
            // Display the value
            System.out.println("\nvalue at Index 0 is : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is greater than"
                               + " the capacity minus 1");
            System.out.println("Exception throws : " + e);
        }
    }
}

输出:

Original CharBuffer:  [a, b, c]

value at Index 0 is : c

例2: 演示IndexOutOfBoundsException。

// Java program to demonstrate
// charAt() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating the CharBuffer
        try {
  
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(3);
  
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                .rewind();
  
            // print the CharBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(
                                     charbuffer.array()));
  
            // Read char at particular Index
            // using charAt() method
            char value = charbuffer.charAt(3);
  
            // Display the value
            System.out.println("\nvalue at Index 0 is : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is greater than"
                               + " the capacity minus 1");
            System.out.println("Exception throws : " + e);
        }
    }
}

输出:

Original CharBuffer:  [a, b, c]

index is greater than the capacity minus 1
Exception throws : java.lang.IndexOutOfBoundsException

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#charAt-int-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南