当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。