Java : FloatBuffer 示例

Java 中的 FloatBuffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 FloatBuffer 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

浮动缓冲区。 (机器翻译)

Class diagram

final var buffer = FloatBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

buffer.put(0.1f);
System.out.println(buffer.position()); // 1

buffer.put(0.2f);
System.out.println(buffer.position()); // 2

buffer.put(0.3f);
System.out.println(buffer.position()); // 3

if (buffer.hasArray()) {
    final var ret = buffer.array();
    System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}
final float[] array = {0.1f, 0.2f, 0.3f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 0.1

System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 0.2

System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 0.3

Methods

static FloatBuffer allocate (int capacity)

分配一个新的浮点缓冲区。 (机器翻译)

final var buffer = FloatBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.0, 0.0, 0.0, 0.0, 0.0]
}

final float[] array ()

返回支持此缓冲区的浮点数组(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(5)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}

final int arrayOffset ()

返回此缓冲区的支持数组中缓冲区第一个元素的偏移量(可选操作)。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

final var sliced = buffer.slice(2, 3);

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0
    System.out.println(buffer.get()); // 0.1
    System.out.println(buffer.get()); // 0.2
    System.out.println(buffer.get()); // 0.3
    System.out.println(buffer.get()); // 0.4
    System.out.println(buffer.get()); // 0.5
}

System.out.println(sliced); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
    System.out.println(sliced.arrayOffset()); // 2
    System.out.println(sliced.get()); // 0.3
    System.out.println(sliced.get()); // 0.4
    System.out.println(sliced.get()); // 0.5
}

abstract FloatBuffer asReadOnlyBuffer ()

创建一个新的、只读的浮点缓冲区,并共享此缓冲区的内容。 (机器翻译)

final var buffer = FloatBuffer.allocate(3);
buffer.put(0.1f);

final var readOnlyBuffer = buffer.asReadOnlyBuffer();

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=1 lim=3 cap=3]
System.out.println(readOnlyBuffer); // java.nio.HeapFloatBufferR[pos=1 lim=3 cap=3]

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 0.1
System.out.println(readOnlyBuffer.get()); // 0.0
System.out.println(readOnlyBuffer.get()); // 0.0

buffer.put(0.2f);

readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 0.1
System.out.println(readOnlyBuffer.get()); // 0.2
System.out.println(readOnlyBuffer.get()); // 0.0

try {
    var _ = readOnlyBuffer.put(0.3f);
} catch (ReadOnlyBufferException e) {
    System.out.println("ReadOnlyBufferException!");
}

// Result
// ↓
//ReadOnlyBufferException!

final FloatBuffer clear ()

清除此缓冲区。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(2);
buffer.limit(4);

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=2 lim=4 cap=5]

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.hasRemaining()); // false

System.out.println(buffer.clear()); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.hasRemaining()); // false

abstract FloatBuffer compact ()

压缩此缓冲区(可选操作)。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(3);
System.out.println(buffer.compact()); // java.nio.HeapFloatBuffer[pos=3 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [0.4, 0.5, 0.6, 0.4, 0.5, 0.6]
final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(2);
System.out.println(buffer.compact()); // java.nio.HeapFloatBuffer[pos=4 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [0.3, 0.4, 0.5, 0.6, 0.5, 0.6]
final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(4);
System.out.println(buffer.compact()); // java.nio.HeapFloatBuffer[pos=2 lim=6 cap=6]

System.out.println(Arrays.toString(array)); // [0.5, 0.6, 0.3, 0.4, 0.5, 0.6]

int compareTo (FloatBuffer that)

将此缓冲区与另一个缓冲区进行比较。 (机器翻译)

final var buffer1 = FloatBuffer.allocate(3)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

final var buffer2 = FloatBuffer.allocate(3)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = FloatBuffer.allocate(3)
        .put(10.0f)
        .put(20.0f)
        .put(30.0f);

final var buffer2 = FloatBuffer.allocate(3)
        .put(5.0f)
        .put(50.0f)
        .put(500.0f);

final var buffer3 = FloatBuffer.allocate(3)
        .put(30.0f)
        .put(20.0f)
        .put(10.0f);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.compareTo(buffer2)); // 1
System.out.println(buffer1.compareTo(buffer3)); // -1

abstract FloatBuffer duplicate ()

创建一个共享此缓冲区内容的新浮点缓冲区。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(1);
buffer.limit(4);

final var duplicated = buffer.duplicate();

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapFloatBuffer[pos=1 lim=4 cap=5]

System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4

System.out.println(duplicated.get()); // 0.2
System.out.println(duplicated.get()); // 0.3
System.out.println(duplicated.get()); // 0.4

boolean equals (Object ob)

判断该缓冲区是否等于另一个对象。 (机器翻译)

final var buffer1 = FloatBuffer.allocate(3)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

final var buffer2 = FloatBuffer.allocate(3)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

buffer1.clear();
buffer2.clear();

System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = FloatBuffer.allocate(3)
        .put(10.0f)
        .put(20.0f)
        .put(30.0f);

final var buffer2 = FloatBuffer.allocate(3)
        .put(5.0f)
        .put(50.0f)
        .put(500.0f);

final var buffer3 = FloatBuffer.allocate(3)
        .put(30.0f)
        .put(20.0f)
        .put(10.0f);

buffer1.clear();
buffer2.clear();
buffer3.clear();

System.out.println(buffer1.equals(buffer2)); // false
System.out.println(buffer1.equals(buffer3)); // false

final FloatBuffer flip ()

翻转此缓冲区。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3

abstract float get ()

相对获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5

System.out.println(buffer.position()); // 5

FloatBuffer get (float[] dst)

相对批量获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 7
System.out.println(buffer.position()); // 0

{
    final var dst = new float[3];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=3 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new float[3];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]

    final var ret = buffer.get(dst);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=6 lim=7 cap=7]

    System.out.println(Arrays.toString(dst)); // [0.4, 0.5, 0.6]
    System.out.println(buffer.position()); // 6
}

try {
    final var dst = new float[3];
    var _ = buffer.get(dst);
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

FloatBuffer get (float[] dst, int offset, int length)

相对批量获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 8
System.out.println(buffer.position()); // 0

{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(dst, 0, 3);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=3 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3, 0.0, 0.0]
    System.out.println(buffer.position()); // 3
}
{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(dst, 1, 3);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=6 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0.0, 0.4, 0.5, 0.6, 0.0]
    System.out.println(buffer.position()); // 6
}
{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(dst, 3, 2);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=8 lim=8 cap=8]

    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.7, 0.8]
    System.out.println(buffer.position()); // 8
}

abstract float get (int index)

绝对获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.get(0)); // 0.1
System.out.println(buffer.get(1)); // 0.2
System.out.println(buffer.get(2)); // 0.3
System.out.println(buffer.get(3)); // 0.4
System.out.println(buffer.get(4)); // 0.5

System.out.println(buffer.position()); // 0

FloatBuffer get (int index, float[] dst)

绝对批量获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

{
    final var dst = new float[3];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]

    final var ret = buffer.get(0, dst);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new float[3];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]

    final var ret = buffer.get(1, dst);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [0.2, 0.3, 0.4]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new float[3];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]

    final var ret = buffer.get(2, dst);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

    System.out.println(Arrays.toString(dst)); // [0.3, 0.4, 0.5]
    System.out.println(buffer.position()); // 0
}

FloatBuffer get (int index, float[] dst, int offset, int length)

绝对批量获取方法。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(0, dst, 0, 3);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3, 0.0, 0.0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(2, dst, 1, 3);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0.0, 0.3, 0.4, 0.5, 0.0]
    System.out.println(buffer.position()); // 0
}
{
    final var dst = new float[5];
    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]

    final var ret = buffer.get(4, dst, 3, 2);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]

    System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.5, 0.6]
    System.out.println(buffer.position()); // 0
}

final boolean hasArray ()

告知此缓冲区是否由可访问的浮点数组支持。 (机器翻译)

final var buffer = FloatBuffer.allocate(5)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f);

final var ret = buffer.hasArray();
System.out.println(ret); // true

if (ret) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}
final var buffer = FloatBuffer.allocate(5)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f)
        .asReadOnlyBuffer();

System.out.println(buffer.hasArray()); // false
try {
    var _ = buffer.array();
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

int hashCode ()

返回此缓冲区的当前哈希码。 (机器翻译)

final var buffer = FloatBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = FloatBuffer.allocate(3)
        .put(0.1f)
        .put(0.2f)
        .put(0.3f)
        .clear();

System.out.println(buffer.hashCode()); // 29791
final var buffer = FloatBuffer.allocate(3)
        .put(-123.0f)
        .put(-456.0f)
        .put(-789.0f)
        .clear();

System.out.println(buffer.hashCode()); // -742697

abstract boolean isDirect ()

告知该浮点缓冲区是否是直接的。 (机器翻译)

final var buffer = FloatBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final float[] array = {0.1f, 0.2f, 0.3f};
final var buffer = FloatBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(8).asFloatBuffer();
System.out.println(buffer.isDirect()); // true

final FloatBuffer limit (int newLimit)

设置此缓冲区的限制。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5

System.out.println(buffer.limit(3)); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3

System.out.println(buffer.clear()); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.limit(3)); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=5]

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3

try {
    var _ = buffer.get();
} catch (BufferUnderflowException e) {
    System.out.println("BufferUnderflowException!");
}

// Result
// ↓
//BufferUnderflowException!

final FloatBuffer mark ()

在此缓冲区的位置设置其标记。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2

System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5

System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5

int mismatch (FloatBuffer that)

查找并返回此缓冲区与给定缓冲区之间第一个不匹配的相对索引。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f};
final var buffer = FloatBuffer.wrap(array);

{
    final float[] a = {0.1f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 1
}
{
    final float[] a = {0.1f, 0.2f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 2
}
{
    final float[] a = {0.1f, 0.2f, 0.3f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 3
}
{
    final float[] a = {0.1f, 0.2f, 0.3f, 0.4f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // -1
}
{
    final float[] a = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 4
}
{
    final float[] a = {0.1f, 0.2f, 0.999f};
    final var that = FloatBuffer.wrap(a); // 2

    System.out.println(buffer.mismatch(that));
}
{
    final float[] a = {-0.9f, -0.8f};
    final var that = FloatBuffer.wrap(a);

    System.out.println(buffer.mismatch(that)); // 0
}

abstract ByteOrder order ()

检索此缓冲区的字节顺序。 (机器翻译)

final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10

final var buffer = FloatBuffer.allocate(5);
System.out.println(buffer.order()); // LITTLE_ENDIAN

final FloatBuffer position (int newPosition)

设置此缓冲区的位置。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.position()); // 0

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.position()); // 1

System.out.println(buffer.get()); // 0.2
System.out.println(buffer.position()); // 2

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.position()); // 3

System.out.println(buffer.get()); // 0.4
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5

System.out.println(buffer.position(3)); // java.nio.HeapFloatBuffer[pos=3 lim=5 cap=5]

System.out.println(buffer.get()); // 0.4
System.out.println(buffer.position()); // 4

System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5

abstract FloatBuffer put (float f)

相对放置方法(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.put(0.1f)); // java.nio.HeapFloatBuffer[pos=1 lim=5 cap=5]
System.out.println(buffer.put(0.2f)); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.put(0.3f)); // java.nio.HeapFloatBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.put(0.4f)); // java.nio.HeapFloatBuffer[pos=4 lim=5 cap=5]
System.out.println(buffer.put(0.5f)); // java.nio.HeapFloatBuffer[pos=5 lim=5 cap=5]

System.out.println(buffer.position()); // 5

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}

final FloatBuffer put (float[] src)

相对批量放入方法(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(6);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final float[] src = {0.1f};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=1 lim=6 cap=6]
}
{
    final float[] src = {0.2f, 0.3f};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=3 lim=6 cap=6]
}
{
    final float[] src = {0.4f, 0.5f, 0.6f};

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=6 lim=6 cap=6]
}

System.out.println(buffer.position()); // 6

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
}

FloatBuffer put (float[] src, int offset, int length)

相对批量放入方法(可选操作)。 (机器翻译)

final var array = new float[6];
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final float[] src = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};

final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1); // java.nio.HeapFloatBuffer[pos=1 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]

final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2); // java.nio.HeapFloatBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]

final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3); // java.nio.HeapFloatBuffer[pos=6 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

System.out.println(buffer.position()); // 6

abstract FloatBuffer put (int index, float f)

绝对put方法(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

System.out.println(buffer.put(0, 0.1f)); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(1, 0.2f)); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(2, 0.3f)); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(3, 0.4f)); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(4, 0.5f)); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}

FloatBuffer put (int index, float[] src)

绝对批量放入方法(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(6);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

{
    final float[] src = {0.1f};

    final var ret = buffer.put(0, src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
}
{
    final float[] src = {0.2f, 0.3f};

    final var ret = buffer.put(1, src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
}
{
    final float[] src = {0.4f, 0.5f, 0.6f};

    final var ret = buffer.put(3, src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
}

System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
}

FloatBuffer put (int index, float[] src, int offset, int length)

绝对批量放入方法(可选操作)。 (机器翻译)

final var array = new float[6];
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final float[] src = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

System.out.println(buffer.position()); // 0

FloatBuffer put (int index, FloatBuffer src, int offset, int length)

绝对批量放入方法(可选操作)。 (机器翻译)

final var array = new float[6];
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0

final float[] srcArray = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
final var src = FloatBuffer.wrap(srcArray);

final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]

final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]

final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapFloatBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

System.out.println(buffer.position()); // 0

FloatBuffer put (FloatBuffer src)

相对批量放入方法(可选操作)。 (机器翻译)

final var buffer = FloatBuffer.allocate(5);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0

{
    final float[] array = {0.1f, 0.2f};
    final var src = FloatBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]
}
{
    final float[] array = {0.3f, 0.4f, 0.5f};
    final var src = FloatBuffer.wrap(array);

    final var ret = buffer.put(src);
    System.out.println(ret); // java.nio.HeapFloatBuffer[pos=5 lim=5 cap=5]
}

System.out.println(buffer.position()); // 5

if (buffer.hasArray()) {
    final var array = buffer.array();
    System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}

final FloatBuffer reset ()

将此缓冲区的位置重置为先前标记的位置。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2

System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5

System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapFloatBuffer[pos=2 lim=5 cap=5]

System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5

final FloatBuffer rewind ()

倒回此缓冲区。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(2);
buffer.limit(4);

System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4

System.out.println(buffer.rewind()); // java.nio.HeapFloatBuffer[pos=0 lim=4 cap=5]

System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4

abstract FloatBuffer slice ()

创建一个新的浮点缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

buffer.position(1);
buffer.limit(4);

final var sliced = buffer.slice();

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 0.2
System.out.println(sliced.get()); // 0.3
System.out.println(sliced.get()); // 0.4
System.out.println(sliced.hasRemaining()); // false

abstract FloatBuffer slice (int index, int length)

创建一个新的浮点缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

final var sliced = buffer.slice(2, 3);

System.out.println(buffer); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapFloatBuffer[pos=0 lim=3 cap=3]

System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.hasRemaining()); // false

System.out.println(sliced.get()); // 0.3
System.out.println(sliced.get()); // 0.4
System.out.println(sliced.get()); // 0.5
System.out.println(sliced.hasRemaining()); // false

String toString ()

返回概括此缓冲区状态的字符串。 (机器翻译)

final var buffer = FloatBuffer.allocate(5)
        .limit(4)
        .position(2);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 4
System.out.println(buffer.position()); // 2

final var str1 = buffer.toString();
System.out.println(str1); // java.nio.HeapFloatBuffer[pos=2 lim=4 cap=5]

buffer.clear();

final var str2 = buffer.toString();
System.out.println(str2); // java.nio.HeapFloatBuffer[pos=0 lim=5 cap=5]

static FloatBuffer wrap (float[] array)

将浮点数组包装到缓冲区中。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
final var buffer = FloatBuffer.wrap(array);

System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0

if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0

    System.out.println(buffer.get()); // 0.1
    System.out.println(buffer.get()); // 0.2
    System.out.println(buffer.get()); // 0.3
    System.out.println(buffer.get()); // 0.4
    System.out.println(buffer.get()); // 0.5
}

static FloatBuffer wrap (float[] array, int offset, int length)

将浮点数组包装到缓冲区中。 (机器翻译)

final float[] array = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f};
final var buffer = FloatBuffer.wrap(array, 2, 4);

System.out.println(buffer.capacity()); // 7
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 2

if (buffer.hasArray()) {
    System.out.println(buffer.arrayOffset()); // 0

    System.out.println(buffer.get()); // 0.3
    System.out.println(buffer.get()); // 0.4
    System.out.println(buffer.get()); // 0.5
    System.out.println(buffer.get()); // 0.6
}

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

请参阅下面的链接。


相关文章

To top of page