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


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


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

用法:

public final DoubleBuffer limit(int 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 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-



相關用法


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