Java ShortBuffer limit()方法及示例

来源:这里教程网 时间:2026-02-17 21:09:12 作者:

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: 2Limit: 4ShortBuffer after setting buffer's limit: [20, 30, 0, 0]Position: 1Limit: 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: 3Limit: 5ShortBuffer before setting buffer's limit: [20, 30, 40, 0, 0]Position: 3Limit: 4

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

相关推荐