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


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


java.nio.IntBuffer類的limit()方法用於修改此IntBuffer的限製。此方法將要設置的限製作為參數,並將其設置為此Buffer的新限製。如果此緩衝區的標記已經定義並且大於新指定的限製,則不會設置並丟棄此新限製。

用法:

public final IntBuffer 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 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-



相關用法


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