Java FloatBuffer limit()方法及示例

Java FloatBuffer limit()方法及示例

java.nio.FloatBuffer类的 limit() 方法是用来修改这个FloatBuffer的极限。这个方法以要设置的极限为参数,并将其设置为这个Buffer的新极限。如果这个Buffer的标记已经被定义,并且大于新指定的极限,那么这个新的极限就不会被设置并被丢弃。

语法

public final FloatBuffer limit(int newLimit)

参数: 该方法需要一个整数类型的参数newLimit,指的是要设置为缓冲区的新极限。

返回值: 该方法在将指定的新极限设置为该缓冲区的新极限后返回该缓冲区。

下面的例子说明了limit()方法的作用。

例子 1 :

// Java program to demonstrate
// limit() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating FloatBuffer
        // using allocate() method
        FloatBuffer floatBuffer
            = FloatBuffer.allocate(4);
  
        // put float value in FloatBuffer
        // using put() method
        floatBuffer.put(20.5f);
        floatBuffer.put(30.5f);
  
        // print the float buffer
        System.out.println(
            "FloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
  
        // Limit the floatBuffer
        // using limit() method
        floatBuffer.limit(1);
  
        // print the float buffer
        System.out.println(
            "\nFloatBuffer after "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
    }
}

输出。

设置缓冲区限制前的FloatBuffer:
[20.5, 30.5, 0.0, 0.0]
位置:2
限制:4

设置缓冲区限制后的FloatBuffer:
[20.5, 30.5, 0.0, 0.0]
位置。1
限制:1

例子2 :

// Java program to demonstrate
// limit() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating FloatBuffer
        // using allocate() method
        FloatBuffer floatBuffer
            = FloatBuffer.allocate(5);
  
        // put float value in FloatBuffer
        // using put() method
        floatBuffer.put(20.5f);
        floatBuffer.put(30.5f);
        floatBuffer.put(40.5f);
  
        // mark will be going to
        // discarded by limit()
        floatBuffer.mark();
  
        // print the float buffer
        System.out.println(
            "FloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
  
        // Limit the floatBuffer
        // using limit() method
        floatBuffer.limit(4);
  
        // print the double buffer
        System.out.println(
            "\nFloatBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  floatBuffer.array())
            + "\nPosition: "
            + floatBuffer.position()
            + "\nLimit: "
            + floatBuffer.limit());
    }
}

输出。

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5

FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 4

参考 :https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#limit-int-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程