废话不多说,直接上代码
package com.ietree.basicskill.socket.basic.nio;
import java.nio.IntBuffer;
public class BufferTest {
public static void main(String[] args) {
// 1 基本操作
// 2 wrap方法使用
// wrap方法会包裹一个数组: 一般这种用法不会先初始化缓存对象的长度,因为没有意义,最后还会被wrap所包裹的数组覆盖掉。
// 并且wrap方法修改缓冲区对象的时候,数组本身也会跟着发生变化。
// 3 其他方法
IntBuffer buf1 = IntBuffer.allocate(10);
int[] arr = new int[]{1,2,5};
buf1.put(arr);
System.out.println(buf1);
//一种复制方法
IntBuffer buf3 = buf1.duplicate();
System.out.println(buf3);
//设置buf1的位置属性
//buf1.position(0);
buf1.flip();
System.out.println(buf1);
System.out.println("可读数据为:" + buf1.remaining());
int[] arr2 = new int[buf1.remaining()];
//将缓冲区数据放入arr2数组中去
buf1.get(arr2);
for(int i : arr2){
System.out.print(Integer.toString(i) + ",");
}
}
}
以上这篇基于IntBuffer类的基本用法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



