📌  相关文章
📜  Java中的 DoubleBuffer limit() 方法及示例

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

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

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

句法:

public final DoubleBuffer limit(int 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 DoubleBuffer
        // using allocate() method
        DoubleBuffer doubleBuffer
            = DoubleBuffer.allocate(4);
  
        // put double value in doubleBuffer
        // using put() method
        doubleBuffer.put(20.5);
        doubleBuffer.put(30.5);
  
        // print the double buffer
        System.out.println("DoubleBuffer before "
                           + "setting buffer's limit: "
                           + Arrays.toString(
                                 doubleBuffer.array())
                           + "\nPosition: "
                           + doubleBuffer.position()
                           + "\nLimit: "
                           + doubleBuffer.limit());
  
        // Limit the doubleBuffer
        // using limit() method
        doubleBuffer.limit(1);
  
        // print the double buffer
        System.out.println("\nDoubleBuffer after "
                           + "setting buffer's limit: "
                           + Arrays.toString(
                                 doubleBuffer.array())
                           + "\nPosition: "
                           + doubleBuffer.position()
                           + "\nLimit: "
                           + doubleBuffer.limit());
    }
}
输出:
DoubleBuffer before setting buffer's limit: [20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4

DoubleBuffer 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 DoubleBuffer
        // using allocate() method
        DoubleBuffer doubleBuffer
            = DoubleBuffer.allocate(5);
  
        // put double value in DoubleBuffer
        // using put() method
        doubleBuffer.put(20.5);
        doubleBuffer.put(30.5);
        doubleBuffer.put(40.5);
  
        // mark will be going to
        // discarded by limit()
        doubleBuffer.mark();
  
        // print the double buffer
        System.out.println("DoubleBuffer before "
                           + "setting buffer's limit: "
                           + Arrays.toString(
                                 doubleBuffer.array())
                           + "\nPosition: "
                           + doubleBuffer.position()
                           + "\nLimit: "
                           + doubleBuffer.limit());
  
        // Limit the doubleBuffer
        // using limit() method
        doubleBuffer.limit(4);
  
        // print the double buffer
        System.out.println("\nDoubleBuffer before "
                           + "setting buffer's limit: "
                           + Arrays.toString(
                                 doubleBuffer.array())
                           + "\nPosition: "
                           + doubleBuffer.position()
                           + "\nLimit: "
                           + doubleBuffer.limit());
    }
}
输出:
DoubleBuffer before setting buffer's limit: [20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5

DoubleBuffer 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/DoubleBuffer.html#limit-int-