Java IntBuffer limit()方法及示例

Java IntBuffer limit()方法及示例

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

语法

public final IntBuffer 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 IntBuffer
        // using allocate() method
        IntBuffer intBuffer
            = IntBuffer.allocate(4);
  
        // put int value in IntBuffer
        // using put() method
        intBuffer.put(20);
        intBuffer.put(30);
  
        // print the int buffer
        System.out.println(
            "IntBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
  
        // Limit the intBuffer
        // using limit() method
        intBuffer.limit(1);
  
        // print the int buffer
        System.out.println(
            "\nintBuffer after "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
    }
}

输出:

IntBuffer before setting buffer's limit: [20, 30, 0, 0]
Position: 2
Limit: 4

intBuffer after setting buffer's limit: [20, 30, 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 IntBuffer
        // using allocate() method
        IntBuffer intBuffer
            = IntBuffer.allocate(5);
  
        // put int value in IntBuffer
        // using put() method
        intBuffer.put(20);
        intBuffer.put(30);
        intBuffer.put(40);
  
        // mark will be going to
        // discarded by limit()
        intBuffer.mark();
  
        // print the int buffer
        System.out.println(
            "intBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
  
        // Limit the intBuffer
        // using limit() method
        intBuffer.limit(4);
  
        // print the int buffer
        System.out.println(
            "\nintBuffer before "
            + "setting buffer's limit: "
            + Arrays.toString(
                  intBuffer.array())
            + "\nPosition: "
            + intBuffer.position()
            + "\nLimit: "
            + intBuffer.limit());
    }
}

输出:

intBuffer before setting buffer's limit: [20, 30, 40, 0, 0]
Position: 3
Limit: 5

intBuffer before setting buffer's limit: [20, 30, 40, 0, 0]
Position: 3
Limit: 4

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程