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–
相关用法
- Java ShortBuffer hasArray()用法及代码示例
- Java ShortBuffer duplicate()用法及代码示例
- Java ShortBuffer compareTo用法及代码示例
- Java ShortBuffer isDirect()用法及代码示例
- Java ShortBuffer asReadOnlyBuffer()用法及代码示例
- Java ShortBuffer arrayOffset()用法及代码示例
- Java ShortBuffer compact()用法及代码示例
- Java ShortBuffer order()用法及代码示例
- Java ShortBuffer toString()用法及代码示例
- Java ShortBuffer array()用法及代码示例
- Java ShortBuffer slice()用法及代码示例
- Java ShortBuffer hashCode()用法及代码示例
- Java ShortBuffer rewind()用法及代码示例
- Java ShortBuffer mark()用法及代码示例
- Java ShortBuffer allocate()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ShortBuffer limit() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。