java 中的 java.nio.FloatBuffer 类
原文:https://www . geesforgeks . org/Java-nio-float buffer-class-in-Java/
缓冲区对象可以被称为固定数据量的容器。缓冲区充当存储箱或临时暂存区,在这里可以存储数据,并在以后根据使用情况进行检索。Java 缓冲类是构建 java.nio 的基础。浮动缓冲区是通过分配(为缓冲区的内容分配空间)、将现有的浮动数组包装到缓冲区或创建现有字节缓冲区的视图来创建的。
这个类定义了浮动缓冲区上的四类操作:
- 绝对和相对 get 和 put 方法读取和写入单个浮点
- 相对大容量 get 方法,将连续的浮点序列从该缓冲区传输到数组中;和
- 相对大容量 put 方法,将连续的浮点序列从浮点数组或其他浮点缓冲区传输到该缓冲区
- 压缩、复制和切片浮动缓冲区的方法。
下面是 java.nio.FloatBuffer 类的一些方法的实现:
1。reset(): 此方法用于将此缓冲区的位置重置到之前标记的位置。
Syntax: public final FloatBuffer reset()
Parameters: None
Return: Returns the buffer.
Java 语言(一种计算机语言,尤用于创建网站)
// Implementation of reset() method in Java
import java.nio.*;
import java.util.*;
public class Example {
public static void main(String[] args)
{
try {
float[] arr = { 10.5f, 20.5f, 30.5f, 40.5f };
// creating object of FloatBuffer
// and allocating size capacity
FloatBuffer x = FloatBuffer.wrap(arr);
// try to set the position at index 2
x.position(2);
// Set this buffer mark position
// using mark() method
x.mark();
// try to set the position at index 4
x.position(4);
// display position
System.out.println("Pos before reset: "
+ x.position());
// try to call reset() to restore
// to the position we marked
x.reset();
// display position
System.out.println("Pos after reset: "
+ x.position());
}
catch (InvalidMarkException e) {
System.out.println("New pos is less than "
+ "the pos "
+ " marked before ");
System.out.println("Exception throws: " + e);
}
}
}
Output
Pos before reset: 4
Pos after reset: 2
2。rewind(): 这个方法用来倒带这个缓冲区。
Syntax: public final FloatBuffer rewind()
Parameters: None
Return: Returns the buffer
Java 语言(一种计算机语言,尤用于创建网站)
// Implementation of rewind() method in Java
import java.nio.*;
import java.util.*;
public class Example2 {
public static void main(String[] args)
{
// defining and allocating FloatBuffer
// using allocate() method
FloatBuffer x = FloatBuffer.allocate(4);
// put char value in FloatBuffer
// using put() method
x.put(10.5f);
x.put(20.5f);
// print the float buffer
System.out.println("Buffer before operation: "
+ Arrays.toString(x.array())
+ "\nPosition: " + x.position()
+ "\nLimit: " + x.limit());
// rewind the Buffer
// using rewind() method
x.rewind();
// print the floatbuffer
System.out.println("\nBuffer after operation: "
+ Arrays.toString(x.array())
+ "\nPosition: " + x.position()
+ "\nLimit: " + x.limit());
}
}
Output
Buffer before operation: [10.5, 20.5, 0.0, 0.0]
Position: 2
Limit: 4
Buffer after operation: [10.5, 20.5, 0.0, 0.0]
Position: 0
Limit: 4
版权属于:月萌API www.moonapi.com,转载请注明出处