java shortbuffer_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值