Java ShortBuffer limit()方法及示例

Java ShortBuffer limit()方法及示例

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

语法

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

输出:

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

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

输出:

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

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

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#mark-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程