下面的代码将通过一个充满bytebuffer的Java中的chars并构建一个string。
StringBuilder actualString = new StringBuilder();
for(int i = 0; i < length; i++)
{
if((char)bbuf.get(i) != '\0');
{
actualString.append((char)bbuf.get(i));
System.out.println("ascii code is " + bbuf.get(i));
}
}代码正确工作,当遇到\0 (null结束符)时,当使用类的调用者获得bytebuffer,并在包含调用函数的地方进行迭代时,代码将停止。
例如
new bufclass = bufclass
buffclass.getbytebuffer
run code from above但是,当我在bytebuffer生成后并在包含bytebuffer的类中分配该代码时,它从未停止在\0字符处。
一个如何做到这一点的例子将是
create bytebuffer and allocate bytebuffer
run code to iterate from above为什么我的代码在使用调用者遍历bytebuffer以获得bytebuffer时工作,而当代码被添加到包含bytebuffer的类时却不能工作。
请注意,这是我打印ASCII字符代码时的结果,是的,我确实尝试了不起作用的if the byte is equal to 0。
ascii code is 116
ascii code is 101
ascii code is 115
ascii code is 116
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0
ascii code is 0发布于 2016-02-04 15:43:54
在if语句之后有一个;:
if((char)bbuf.get(i) != '\0');这在功能上相当于
if((char)bbuf.get(i) != '\0') {
}所以,去掉那个;,你就可以走了。
https://stackoverflow.com/questions/35204674
复制相似问题