java intbuffer用法_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值