📜  Java中的 CharBuffer wrap() 方法

📅  最后修改于: 2022-05-13 01:54:34.868000             🧑  作者: Mango

Java中的 CharBuffer wrap() 方法

换行(字符 [] 数组)

Java.nio.CharBuffer类的wrap()方法用于将字符数组包装到缓冲区中。新缓冲区将由给定的 char 数组支持。因此,对缓冲区的任何修改都会导致数组被修改,反之亦然。新缓冲区的容量由array.length确定,其位置为零,其标记未定义。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法:

public static CharBuffer wrap(char[] array)

参数:此方法接受一个数组作为参数,该参数将支持此缓冲区。

返回值:此方法返回新的字符缓冲区。

下面是说明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[ ] 数组,int 偏移量,int 长度)

新缓冲区将由给定的 char 数组支持。因此,对缓冲区的任何修改都会导致数组被修改,反之亦然。新缓冲区的容量由array.length决定,它的位置是偏移量,它的限制是偏移量+长度,它的标记是未定义的。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法:

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

参数:此方法采用以下参数:

  • 数组:将支持新缓冲区的数组。
  • offset:要使用的子数组的偏移量;必须为非负且不大于 array.length。新缓冲区的位置将设置为此值。
  • length:要使用的子数组的长度;必须为非负数且不大于 array.length – 偏移量。新缓冲区的限制将设置为偏移量 + 长度。

返回值:此方法返回新的字符缓冲区。

抛出:此方法抛出IndexOutOfBoundsException (如果偏移量和长度参数的前提条件不成立)。

下面是说明 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