java 遍历问题为什么会抛异常

public class GetData {
private static final int BSIZE = 1024;
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.allocate(BSIZE);
int i = 0;
while(i++ < bb.limit()){
System.out.println(i);
if(bb.get() != 0){
System.out.println("nonzero");
}else{
if(i == 512){
System.out.println(bb.get());
}
}
}

}

}

结果Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:492)
at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:135)
at java_nio.GetData.main(GetData.java:13)

2个回答

每次调用bb.get()方法position都会移动指针

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!