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


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


put(char i)

java.nio.CharBuffer類的put(char i)方法用於將給定字符寫入當前位置的新創建的char緩衝區中,然後遞增該位置。

用法:

public abstract CharBuffer put(char i)

參數:此方法將char值i作為要寫入char緩衝區的參數。


返回值:此方法返回此緩衝區,在其中插入char值。

異常:此方法引發以下異常:

  • BufferOverflowException-如果此緩衝區的當前位置不小於其限製
  • ReadOnlyBufferException-如果此緩衝區是隻讀的

以下是說明put(char i)方法的示例:

範例1:

// Java program to demonstrate 
// put() 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 using put() method 
            cb.put('a'); 
            cb.put('b'); 
            cb.put('c'); 
            cb.rewind(); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
        } 
  
        catch (BufferOverflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c]

範例2:演示BufferOverflowException。

// Java program to demonstrate 
// put() 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 using put() method 
            cb.put('a'); 
            cb.put('b'); 
            cb.put('c'); 
  
            System.out.println("Trying to put the Int at the "
                               + "position more than its limit"); 
            cb.put('d'); 
  
            // rewind the Charbuffer 
            cb.rewind(); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
        } 
  
        catch (BufferOverflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Trying to put the Int at the position more than its limit
Exception throws:java.nio.BufferOverflowException

範例3:演示ReadOnlyBufferException。

// Java program to demonstrate 
// put() 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 using allocate() method 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // Creating a read-only copy of CharBuffer 
            // using asReadOnlyBuffer() method 
            CharBuffer cb1 = cb.asReadOnlyBuffer(); 
  
            System.out.println("Trying to put the Int value"
                               + " in read only buffer"); 
  
            // putting the value in readonly Charbuffer 
            // using put() method 
            cb1.put('a'); 
        } 
  
        catch (BufferOverflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Trying to put the Int value in read only buffer
Exception throws:java.nio.ReadOnlyBufferException

put(int index, char i)

java.nio.CharBuffer類的put(int index,char i)方法用於將給定的int寫入給定索引處的緩衝區。


用法:

public abstract CharBuffer put(int index, char i)

參數:此方法將以下參數作為參數:

  • index:將要寫入char的索引
  • i:要寫入的char值

返回值:此方法返回此緩衝區。

異常:此方法引發以下異常:

  • IndexOutOfBoundsException-如果索引為負或不小於緩衝區的限製
  • ReadOnlyBufferException-如果此緩衝區是隻讀的

以下示例說明了put(int index,char i)方法:

範例1:

// Java program to demonstrate 
// put() 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 using put() at index 0 
            cb.put(0, 'a'); 
  
            // putting the value in Charbuffer using put() at index 2 
            cb.put(2, 'c'); 
  
            // putting the value in Charbuffer using put() at index 1 
            cb.put(1, 'b'); 
  
            // rewinding the Charbuffer 
            cb.rewind(); 
  
            // print the CharBuffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString(cb.array())); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c]

範例2:演示IndexOutOfBoundsException。

// Java program to demonstrate 
// put() 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 
            // using put() at index 0 
            cb.put(0, 'c'); 
  
            // putting the value in Charbuffer 
            // using put() at index 2 
            cb.put(2, 'b'); 
  
            // putting the value in Charbuffer 
            // using put() at index -1 
            System.out.println("Trying to put the value"
                               + " at the negative index"); 
            cb.put(-1, 'a'); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Trying to put the value at the negative index
Exception throws:java.lang.IndexOutOfBoundsException

範例3:演示ReadOnlyBufferException。

// Java program to demonstrate 
// put() 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 using allocate() method 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // Creating a read-only copy of CharBuffer 
            // using asReadOnlyBuffer() method 
            CharBuffer cb1 = cb.asReadOnlyBuffer(); 
  
            System.out.println("Trying to put the Int value"
                               + " in read only buffer"); 
  
            // putting the value in readonly Charbuffer 
            // using put() method 
            cb1.put(0, 'a'); 
        } 
  
        catch (BufferOverflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Trying to put the Int value in read only buffer
Exception throws:java.nio.ReadOnlyBufferException


相關用法


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