当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。