换行(int[]数组)
这个 包裹() 方法 JAVA尼奥。IntBuffer 类用于将int数组包装到缓冲区中。新的缓冲区将由给定的int数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量和限制将是数组。长度,其位置为零,其标记未定义。其支持数组将是给定的数组,其数组偏移量将为零。
null
语法:
public static IntBuffer wrap(int[] array)
参数: 这种方法需要 大堆 作为一个参数,它是将支持此缓冲区的数组。
返回值: 此方法返回 新的int缓冲区 创建。
下面是一些例子来说明 包裹() 方法:
例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 int array int [] ibb = { 1 , 2 , 3 }; // print the int array length System.out.println( "Array length : " + ibb.length); // print the int array element System.out.println( "Array element : " + Arrays.toString(ibb)); // wrap the int array into intBuffer // using wrap() method IntBuffer intBuffer = IntBuffer.wrap(ibb); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println( "intBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println( "intbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println( "intbuffer position: " + intBuffer.position()); } } |
输出:
Array length : 3 Array element : [1, 2, 3] intBuffer : [1, 2, 3] intbuffer capacity : 3 intbuffer position: 0
换行(int[]数组,int偏移量,int长度)
wrap()方法将int数组包装到缓冲区中。新的缓冲区将由给定的int数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量将是数组。长度,其位置将偏移,其限制将为偏移+长度,其标记将未定义。其支持数组将是给定的数组,其数组偏移量将为零。
语法:
public static IntBuffer wrap (int[] array, int offset, int 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 int array int [] ibb = { 1 , 2 , 3 }; // print the int array length System.out.println( "Array length : " + ibb.length); // print the int array element System.out.println( "Array element : " + Arrays.toString(ibb)); // wrap the int array into intBuffer // using wrap() method IntBuffer intBuffer = IntBuffer.wrap(ibb, 0 , ibb.length); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println( "intBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println( "intbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println( "intbuffer position: " + intBuffer.position()); } } |
输出:
Array length : 3 Array element : [1, 2, 3] intBuffer : [1, 2, 3] intbuffer capacity : 3 intbuffer 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 int [] ibb = { 1 , 2 , 3 }; // print the int array length System.out.println( "Array length : " + ibb.length); // print the int array element System.out.println( "Array element : " + Arrays.toString(ibb)); try { // wrap the int array into intBuffer // using wrap() method System.out.println( "Here " + "offset and length does not hold" + " the required condition " ); IntBuffer intBuffer = IntBuffer.wrap(ibb, 1 , ibb.length); // Rewind the intbuffer intBuffer.rewind(); // print the int buffer System.out.println( "intBuffer : " + Arrays.toString(intBuffer.array())); // print the IntBuffer capacity System.out.println( "intbuffer capacity : " + intBuffer.capacity()); // print the IntBuffer position System.out.println( "intbuffer position: " + intBuffer.position()); } catch (IndexOutOfBoundsException e) { System.out.println( "Exception throws: " + e); } } } |
输出:
Array length : 3 Array element : [1, 2, 3] Here offset and length does not hold the required condition Exception throws: java.lang.IndexOutOfBoundsException
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END