import java.io.*;
import java.nio.*;
public class BatchWriter {
public static void main(String[] args)throws IOException {
ByteBuffer bb=ByteBuffer.allocate(1024);
IntBuffer ib=bb.asIntBuffer();
ib.put(new int[]{10,20,30,40,50});
System.out.println(ib.get(3)); ib.put(3, 100);
System.out.println(ib.get(3)); ib.rewind();
while(ib.hasRemaining()){
int i=ib.get();
if(i==0) break;
System.out.println(i);
}
}
} 上面结果打印
40
100
10
20
30
100
50 但我把ib.rewind()去掉,为什么就只打印
40
100 当前位置不是3吗,50为什么不打印了? 谢谢
import java.nio.*;
public class BatchWriter {
public static void main(String[] args)throws IOException {
ByteBuffer bb=ByteBuffer.allocate(1024);
IntBuffer ib=bb.asIntBuffer();
ib.put(new int[]{10,20,30,40,50});
System.out.println(ib.get(3)); ib.put(3, 100);
System.out.println(ib.get(3)); ib.rewind();
while(ib.hasRemaining()){
int i=ib.get();
if(i==0) break;
System.out.println(i);
}
}
} 上面结果打印
40
100
10
20
30
100
50 但我把ib.rewind()去掉,为什么就只打印
40
100 当前位置不是3吗,50为什么不打印了? 谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货