當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ShortBuffer limit()用法及代碼示例


java.nio.ShortBuffer類的limit()方法用於修改此ShortBuffer的限製。此方法將要設置的限製作為參數,並將其設置為此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–



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ShortBuffer limit() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。