java doublebuffer_Java DoubleBuffer get()用法及代码示例

java.nio.DoubleBuffer类的get()方法用于读取给定缓冲区当前位置的double,然后递增该位置。

用法:

public abstract double get()

返回值:此方法返回缓冲区当前位置的double值。

异常:如果缓冲区的当前位置不小于其限制,则此方法将引发BufferUnderflowException,然后将引发此异常。

下面是说明get()方法的示例:

范例1:

// Java program to demonstrate

// get() method

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the capacity of the FloatBuffer

int capacity = 5;

// Creating the FloatBuffer

try {

// creating object of floatbuffer

// and allocating size capacity

DoubleBuffer fb

= DoubleBuffer.allocate(capacity);

// putting the value in Doublebuffer

fb.put(8.56D);

fb.put(9.61D);

fb.put(1.24D);

fb.rewind();

// print the DoubleBuffer

System.out.println("Original DoubleBuffer:  "

+ Arrays.toString(fb.array()));

// Reads the double at this buffer's current position

// using get() method

double value = fb.get();

// print the double value

System.out.println("\nDouble Value:"

+ value);

// Reads the  double at this buffer's next position

// using get() method

double value1 = fb.get();

// print the double value

System.out.print("\nNext double Value:" + value1);

}

catch (IllegalArgumentException e) {

System.out.println("\nIllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("\nReadOnlyBufferException catched");

}

catch (BufferUnderflowException e) {

System.out.println("\nException throws:" + e);

}

}

}

输出:

Original DoubleBuffer: [8.56, 9.61, 1.24, 0.0, 0.0]

Double Value:8.56

Next double Value:9.61

范例2:

// Java program to demonstrate

// get() 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 fb = DoubleBuffer.allocate(capacity);

// putting the value in Doublebuffer

fb.put(8.56F);

fb.put(9.61F);

// print the DoubleBuffer

System.out.println("Original DoubleBuffer:  "

+ Arrays.toString(fb.array()));

// Reads the Double at this buffer's current position

// using get() method

Double value = fb.get();

// print the Double value

System.out.println("\nDouble Value:" + value);

// Reads the  Double at this buffer's next position

// using get() method

System.out.print("\nsince the buffer current"

+ " position is incremented");

System.out.print(" to greater than its limit ");

Double value1 = fb.get();

// print the Double value

System.out.print("\nNext Double Value:" + value1);

}

catch (IllegalArgumentException e) {

System.out.println("\nIllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("\nReadOnlyBufferException catched");

}

catch (BufferUnderflowException e) {

System.out.println("\nException throws:" + e);

}

}

}

输出:

Original DoubleBuffer: [8.5600004196167, 9.609999656677246, 0.0]

Double Value:0.0

since the buffer current position is incremented to greater than its limit

Exception throws:java.nio.BufferUnderflowException

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值