Java DoubleBuffer put()方法及实例
put(double f)
java.nio.DoubleBuffer 类的 put(double f) 方法用于将给定的双倍数写入新创建的双倍数缓冲区的当前位置,然后递增该位置。
语法
public abstract DoubleBuffer put(double f)
参数: 该方法以双倍值 f 为参数,该值将被写入双倍缓冲区中。
返回值: 该方法返回 这个缓冲区 ,其中插入了双倍值。
异常: 该方法会抛出以下异常。
- BufferOverflowException – 如果这个缓冲区的当前位置不比它的极限值小。
- ReadOnlyBufferException – 如果这个缓冲区是只读的。
下面是说明put(double f)方法的例子:
例1 :
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 3;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer using put() method
db.put(8.56D);
db.put(9.61D);
db.put(7.86D);
db.rewind();
// print the DoubleBuffer
System.out.println("Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Original DoubleBuffer: [8.56, 9.61, 7.86]
例2: 为了证明BufferOverflowException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 3;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer using put() method
db.put(8.56F);
db.put(9.61F);
db.put(7.86F);
System.out.println("Trying to put the Double at the "
+ "position more than its limit");
db.put(7.86F);
// rewind the Doublebuffer
db.rewind();
// print the DoubleBuffer
System.out.println("Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Trying to put the Double at the position more than its limit
Exception throws : java.nio.BufferOverflowException
实例3: 为了证明ReadOnlyBufferException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of theDoubleBuffer
int capacity = 3;
// Creating theDoubleBuffer
try {
// creating object ofDoublebuffer
// and allocating size capacity using allocate() method
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// Creating a read-only copy ofDoubleBuffer
// using asReadOnlyBuffer() method
DoubleBuffer db1 = db.asReadOnlyBuffer();
System.out.println("Trying to put theDouble value"
+ " in read only buffer");
// putting the value in readonlyDoublebuffer
// using put() method
db1.put(8.56F);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Trying to put theDouble value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
put(int index, double f)
java.nio.DoubleBuffer 类的 put(int index, double f) 方法是用来将给定的双倍数写入给定索引的缓冲区中。
语法
public abstract DoubleBuffer put(int index, double f)
参数: 该方法接受以下参数作为参数。
- index : 将被写入双数的索引
- f : 要写入的双倍值
返回值 :该方法返回 该缓冲区 。
异常情况。这个方法会抛出以下异常。
- IndexOutOfBoundsException – 如果索引是负数或者不小于缓冲区的极限值。
- ReadOnlyBufferException – 如果这个缓冲区是只读的。
下面是说明put(int index, double f)方法的例子。
例1 :
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 3;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer using put() at index 0
db.put(0, 8.56F);
// putting the value in Doublebuffer using put() at index 2
db.put(2, 9.61F);
// putting the value in Doublebuffer using put() at index 1
db.put(1, 7.86F);
// rewinding the Doublebuffer
db.rewind();
// print the DoubleBuffer
System.out.println("Original DoubleBuffer: "
+ Arrays.toString(db.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Original DoubleBuffer: [8.5600004196167, 7.860000133514404, 9.609999656677246]
例2: 演示IndexOutOfBoundsException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 3;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer
// using put() at index 0
db.put(0, 8.56F);
// putting the value in Doublebuffer
// using put() at index 2
db.put(2, 9.61F);
// putting the value in Doublebuffer
// using put() at index -1
System.out.println("Trying to put the value"
+ " at the negative index");
db.put(-1, 7.86F);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
例3: 演示ReadOnlyBufferException。
// Java program to demonstrate
// put() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 3;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity using allocate() method
DoubleBuffer db = DoubleBuffer.allocate(capacity);
// Creating a read-only copy of DoubleBuffer
// using asReadOnlyBuffer() method
DoubleBuffer db1 = db.asReadOnlyBuffer();
System.out.println("Trying to put the Double value"
+ " in read only buffer");
// putting the value in readonly Doublebuffer
// using put() method
db1.put(0, 8.56F);
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Trying to put the Double value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException