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


Java CharBuffer get()用法及代碼示例


java.nio.CharBuffer類的get()方法用於讀取給定緩衝區當前位置的char,然後遞增該位置。

用法:

public abstract char get()

返回值:此方法返回緩衝區當前位置的char值。


異常:此方法拋出BufferUnderflowException-如果緩衝區的當前位置不小於其限製,則拋出此異常。

下麵是說明get()方法的示例:

範例1:

// Java program to demonstrate 
// get() method 
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 5; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Charbuffer 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // putting the value in Charbuffer 
            cb.put('a'); 
            cb.put('b'); 
            cb.put('c'); 
            cb.rewind(); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
  
            // Reads the Int at this buffer's current position 
            // using get() method 
            char value = cb.get(); 
  
            // print the Int value 
            System.out.println("Int Value:" + value); 
  
            // Reads the Int at this buffer's next position 
            // using get() method 
            char value1 = cb.get(); 
  
            // print the Int value 
            System.out.print("Next Int Value:" + value1); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c, , ]
Int Value:a
Next Int Value:b

範例2:

// Java program to demonstrate 
// get() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 3; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Charbuffer 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // putting the value in Charbuffer 
            cb.put('a'); 
            cb.put('b'); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
  
            // Reads the Char at this buffer's current position 
            // using get() method 
            char value = cb.get(); 
  
            // print the Char value 
            System.out.println("Char Value:" + value); 
  
            // Reads the Char at this buffer's next position 
            // using get() method 
            System.out.print("Since the buffer current position is incremented\n"); 
            System.out.print(" to greater than its limit "); 
  
            char value1 = cb.get(); 
  
            // print the Char value 
            System.out.print("Next Char Value:" + value1); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, ]
Char Value:
Since the buffer current position is incremented
to greater than its limit Exception throws:java.nio.BufferUnderflowException

get(int index)

CharBuffer的get(int index)方法用於讀取指定索引處的文章。

用法:

public abstract char get(int index)

參數:此方法將索引(將從中讀取char的索引)作為參數。

返回值:此方法返回給定索引處的char值。


異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。

以下示例說明了get(int index)方法:

範例1:

// Java program to demonstrate 
// get(int index) method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 3; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Charbuffer 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // putting the value in Charbuffer 
            cb.put('a'); 
            cb.put('b'); 
            cb.put('c'); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
  
            // Reads the Char at the index 0 of the Charbuffer 
            // using get() method 
            char value0 = cb.get(0); 
  
            // print the Char value 
            System.out.println("Char Value at index 0:" + value0); 
  
            // Reads the Char at the index 1 of the Charbuffer 
            // using get() method 
            char value1 = cb.get(1); 
  
            // print the Char value 
            System.out.println("Char Value at index 1:" + value1); 
  
            // Reads the Char at the index 2 of the Charbuffer 
            // using get() method 
            char value2 = cb.get(2); 
  
            // print the Char value 
            System.out.println("Char Value at index 2:" + value2); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c]
Char Value at index 0:a
Char Value at index 1:b
Char Value at index 2:c

範例2:

// Java program to demonstrate 
// get() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 3; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Charbuffer 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // putting the value in Charbuffer 
            cb.put('a'); 
            cb.put('b'); 
            cb.put('c'); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
  
            // Reads the Char at the index 0 of the Charbuffer 
            // using get() method 
            char value0 = cb.get(0); 
  
            // print the Char value 
            System.out.println("Char Value at index 0:" + value0); 
  
            // Reads the Char at the index 1 of the Charbuffer 
            // using get() method 
            char value1 = cb.get(1); 
  
            // print the Char value 
            System.out.println("Char Value at index 1:" + value1); 
  
            // Reads the Char at the index 2 of the Charbuffer 
            // using get() method 
            System.out.println("Trying to get the Char"
                               + " of index greater than its limit "); 
            char value2 = cb.get(4); 
  
            // print the Char value 
            System.out.println("Char Value at index 2:" + value2); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c]
Char Value at index 0:a
Char Value at index 1:b
Trying to get the Char of index greater than its limit 
Exception thrown:java.lang.IndexOutOfBoundsException


相關用法


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