📜  Java中的 IntBuffer limit() 方法及示例

📅  最后修改于: 2022-05-13 01:55:17.373000             🧑  作者: Mango

Java中的 IntBuffer limit() 方法及示例

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

句法:

public final IntBuffer limit(int newLimit)

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

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

以下是说明 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-