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


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


wrap(char[ ] array)

java.nio.CharBuffer類的wrap()方法用於將字符數組包裝到緩衝區中。新緩衝區將由給定的char數組支持。結果,對緩衝區的任何修改都將導致數組被修改,反之亦然。新緩衝區的容量由array.length確定,其位置將為零,並且其標記將不確定。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static CharBuffer wrap(char[] array)

參數:此方法接受一個數組作為參數,它將支持此緩衝區。


返回值:此方法返回新的char緩衝區。

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

示例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the char array 
        char[] cbb = { 'a', 'b', 'c' }; 
  
        // print the char array length 
        System.out.println("Array length : " + cbb.length); 
  
        // print the char array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(cbb)); 
  
        // wrap the char array into charBuffer 
        // using wrap() method 
        CharBuffer charBuffer = CharBuffer.wrap(cbb); 
  
        // Rewind the intbuffer 
        charBuffer.rewind(); 
  
        // print the char buffer 
        System.out.println("\ncharBuffer : "
                           + Arrays.toString(charBuffer.array())); 
  
        // print the CharBuffer capacity 
        System.out.println("\ncharbuffer capacity : "
                           + charBuffer.capacity()); 
  
        // print the CharBuffer position 
        System.out.println("\ncharbuffer position: "
                           + charBuffer.position()); 
    } 
}
輸出:
Array length : 3

Array element : [a, b, c]

charBuffer : [a, b, c]

charbuffer capacity : 3

charbuffer position: 0

wrap(char[ ] array, int offset, int length)

新緩衝區將由給定的char數組支持。結果,對緩衝區的任何修改都將導致數組被修改,反之亦然。新緩衝區的容量由array.length決定,其位置將偏移,其限製將為offset + length,並且其標記將是不確定的。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static CharBuffer wrap(char[ ] array, int offset, int length)

參數:此方法采用以下參數:

  • array:將支持新緩衝區的數組。
  • offset:要使用的子數組的偏移量;必須為非負數,且不得大於array.length。新緩衝區的位置將設置為此值。
  • length:要使用的子數組的長度;必須為非負且不大於array.length –偏移量。新緩衝區的限製將設置為偏移量+長度。

返回值:此方法返回新的char緩衝區。

拋出:此方法拋出IndexOutOfBoundsException(如果offset和length參數的前提條件不成立)。

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

示例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the char array 
        char[] cbb = { 'a', 'b', 'c' }; 
  
        // print the char array length 
        System.out.println("Array length : " + cbb.length); 
  
        // print the char array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(cbb)); 
  
        // wrap the char array into charBuffer 
        // using wrap() method 
        CharBuffer charBuffer = CharBuffer.wrap(cbb, 0, cbb.length); 
        // Rewind the intbuffer 
        charBuffer.rewind(); 
  
        // print the char buffer 
        System.out.println("\ncharBuffer : "
                           + Arrays.toString(charBuffer.array())); 
  
        // print the CharBuffer capacity 
        System.out.println("\ncharbuffer capacity : "
                           + charBuffer.capacity()); 
  
        // print the CharBuffer position 
        System.out.println("\ncharbuffer position: "
                           + charBuffer.position()); 
    } 
}
輸出:
Array length : 3

Array element : [a, b, c]

charBuffer : [a, b, c]

charbuffer capacity : 3

charbuffer position: 0

範例2:演示NullPointerException

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the float array 
        char[] cbb = { 'a', 'b', 'c' }; 
  
        // print the char array length 
        System.out.println("Array length : " + cbb.length); 
  
        // print the char array element 
        System.out.println("\nArray element : " + Arrays.toString(cbb)); 
  
        try { 
            // wrap the char array into charBuffer 
            // using wrap() method 
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition "); 
            CharBuffer charBuffer = CharBuffer.wrap(cbb, 1, cbb.length); 
  
            // Rewind the intbuffer 
            charBuffer.rewind(); 
  
            // print the char buffer 
            System.out.println("\ncharBuffer : "
                               + Arrays.toString(charBuffer.array())); 
  
            // print the CharBuffer capacity 
            System.out.println("\ncharbuffer capacity : "
                               + charBuffer.capacity()); 
  
            // print the CharBuffer position 
            System.out.println("\ncharbuffer position: "
                               + charBuffer.position()); 
        } 
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception throws: " + e); 
        } 
    } 
}
輸出:
Array length : 3

Array element : [a, b, c]

Here offset and length does not hold the required condition 
Exception throws: java.lang.IndexOutOfBoundsException


相關用法


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