wrap(double[] array)
java.nio.DoubleBuffer類的wrap()方法用於將雙精度數組包裝到緩衝區中。新緩衝區將由給定的double數組支持;也就是說,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量和限製為array.length,位置為零,標記未定義。它的支持數組將是給定的數組,其數組偏移量將為零。
用法:
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("\nArray 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("\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
// print the DoubleBuffer capacity
System.out.println("\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
// print the DoubleBuffer position
System.out.println("\ndoublebuffer 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
wrap(double[] array, int offset, int length)
新緩衝區將由給定的double數組支持;也就是說,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量將為array.length,其位置將偏移,其限製將為offset + length,其標記將是不確定的。它的支持數組將是給定的數組,其數組偏移量將為零。
用法:
public static FloatBuffer wrap (double[] array, int offset, int length)
參數:此方法采用以下參數:
- array:將支持新緩衝區的數組。
- offset:要使用的子數組的偏移量;必須為非負數,且不得大於array.length。新緩衝區的位置將設置為此值。
- length:要使用的子數組的長度;必須為非負且不大於array.length –偏移量。新緩衝區的限製將設置為偏移量+長度。
返回值:此方法返回新的雙緩衝區。
異常:如果offset和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("\nArray 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("\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
// print the FloatBuffer capacity
System.out.println("\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
// print the FloatBuffer position
System.out.println("\ndoublebuffer 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("\nArray element : " + Arrays.toString(fbb));
try {
// wrap the Double array into floatBuffer
// using wrap() method
System.out.println("\nHere "
+ "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("\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
// print the DoubleBuffer capacity
System.out.println("\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
// print the DoubleBuffer position
System.out.println("\ndoublebuffer 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
相關用法
- Java DoubleBuffer arrayOffset()用法及代碼示例
- Java DoubleBuffer array()用法及代碼示例
- Java DoubleBuffer duplicate()用法及代碼示例
- Java DoubleBuffer allocate()用法及代碼示例
- Java DoubleBuffer hasArray()用法及代碼示例
- Java DoubleBuffer equals()用法及代碼示例
- Java DoubleBuffer compareTo()用法及代碼示例
- Java DoubleBuffer asReadOnlyBuffer()用法及代碼示例
- Java DoubleBuffer slice()用法及代碼示例
- Java DoubleBuffer compact()用法及代碼示例
- Java DoubleBuffer rewind()用法及代碼示例
- Java ByteBuffer wrap()用法及代碼示例
- Java FloatBuffer wrap()用法及代碼示例
- Java DoubleBuffer put()方法用法及代碼示例
- Java DoubleBuffer get()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer wrap() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。