当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java FloatBuffer limit()用法及代码示例


java.nio.FloatBuffer类的limit()方法用于修改此FloatBuffer的限制。此方法将要设置的限制作为参数,并将其设置为此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 before setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4

FloatBuffer after setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 1
Limit: 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-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 FloatBuffer limit() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。