换行(双[]数组)
这个 包裹() 方法 JAVA尼奥。双重增益 er类用于将双数组包装到缓冲区中。新的缓冲区将由给定的双数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量和限制将是数组。长度,其位置为零,其标记未定义。其支持数组将是给定的数组,其数组偏移量将为零。
null
语法:
public static DoubleBuffer wrap(double[] array)
参数: 这种方法需要 大堆 ,将作为参数支持此缓冲区的数组。
返回值: 此方法返回 新的双缓冲区。
下面是演示wrap()方法的示例:
// Java program to demonstrate // wrap() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaire and initialize the float array double [] fbb = { 1 .23D, 2 .34D, 4 .56D }; // print the float array length System.out.println( "Array length : " + fbb.length); // print the float array element System.out.println( "Array element : " + Arrays.toString(fbb)); // wrap the float array into floatBuffer // using wrap() method DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb); // Rewind the floatbuffer doubleBuffer.rewind(); // print the float buffer System.out.println( "doubleBuffer : " + Arrays.toString(doubleBuffer.array())); // print the DoubleBuffer capacity System.out.println( "doublebuffer capacity : " + doubleBuffer.capacity()); // print the DoubleBuffer position System.out.println( "doublebuffer position: " + doubleBuffer.position()); } } |
输出:
Array length : 3 Array element : [1.23, 2.34, 4.56] doubleBuffer : [1.23, 2.34, 4.56] doublebuffer capacity : 3 doublebuffer position: 0
换行(双[]数组,整数偏移,整数长度)
新的缓冲区将由给定的双数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量将是数组。长度,其位置将偏移,其限制将为偏移+长度,其标记将未定义。其支持数组将是给定的数组,其数组偏移量将为零。
语法:
public static FloatBuffer wrap (double[] 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 float array double [] fbb = { 1 .23D, 2 .34D, 4 .56D }; // print the float array length System.out.println( "Array length : " + fbb.length); // print the float array element System.out.println( "Array element : " + Arrays.toString(fbb)); // wrap the double array into floatBuffer // using wrap() method DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 0 , fbb.length); // Rewind the doublebuffer doubleBuffer.rewind(); // print the float buffer System.out.println( "doubleBuffer : " + Arrays.toString(doubleBuffer.array())); // print the FloatBuffer capacity System.out.println( "doublebuffer capacity : " + doubleBuffer.capacity()); // print the FloatBuffer position System.out.println( "doublebuffer position: " + doubleBuffer.position()); } } |
输出:
Array length : 3 Array element : [1.23, 2.34, 4.56] doubleBuffer : [1.23, 2.34, 4.56] doublebuffer capacity : 3 doublebuffer position: 0
例2: 展示IndexOutOfBoundsException
// Java program to demonstrate // asReadOnlyBuffer() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declare and initialize the Double array double [] fbb = { 1 .23D, 2 .34D, 4 .56D }; // print the Double array length System.out.println( "Array length : " + fbb.length); // print the Double array element System.out.println( "Array element : " + Arrays.toString(fbb)); try { // wrap the Double array into floatBuffer // using wrap() method System.out.println( "Here " + "offset and length does not hold" + " the required condition " ); DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 1 , fbb.length); // Rewind the Doublebuffer doubleBuffer.rewind(); // print the Double buffer System.out.println( "doubleBuffer : " + Arrays.toString(doubleBuffer.array())); // print the DoubleBuffer capacity System.out.println( "doublebuffer capacity : " + doubleBuffer.capacity()); // print the DoubleBuffer position System.out.println( "doublebuffer position: " + doubleBuffer.position()); } catch (IndexOutOfBoundsException e) { System.out.println( "Exception throws: " + e); } } } |
输出:
Array length : 3 Array element : [1.23, 2.34, 4.56] Here offset and length does not hold the required condition Exception throws: java.lang.IndexOutOfBoundsException
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END