java数组内存偏移量,Java:CharBuffer“忽略”数组偏移量?

请帮助我理解以下内容:

我创建了一个

CharBuffer

使用

CharBuffer.wrapped(new char[12], 2, 10)

(数组、偏移、长度)

因此,我希望使用偏移量

2

以及

10

. 但是

arrayOffset()

收益率

0

.

我想了解的是(并且无法从JavaDoc中了解到):

什么时候

数组()

不是

有没有可能让

字符缓冲器

使用具有“实际”偏移量的数组(这样就永远不会在该偏移量之前访问该数组)?

下面是一个小测试用例:

import java.nio.*;

import java.util.*;

import org.junit.*;

public class _CharBufferTests {

public _CharBufferTests() {

}

private static void printBufferInfo(CharBuffer b) {

System.out.println("- - - - - - - - - - - - - - -");

System.out.println("capacity: " + b.capacity());

System.out.println("length: " + b.length());

System.out.println("arrayOffset: " + b.arrayOffset());

System.out.println("limit: " + b.limit());

System.out.println("position: " + b.position());

System.out.println("remaining: " + b.remaining());

System.out.print("content from array: ");

char[] array = b.array();

for (int i = 0; i < array.length; ++i) {

if (array[i] == 0) {

array[i] = '_';

}

}

System.out.println(Arrays.toString(b.array()));

}

@Test

public void testCharBuffer3() {

CharBuffer b = CharBuffer.wrap(new char[12], 2, 10);

printBufferInfo(b);

b.put("abc");

printBufferInfo(b);

b.rewind();

b.put("abcd");

printBufferInfo(b);

}

}

输出:

- - - - - - - - - - - - - - -

capacity: 12

length: 10

arrayOffset: 0

limit: 12

position: 2

remaining: 10

content from array: [_, _, _, _, _, _, _, _, _, _, _, _]

- - - - - - - - - - - - - - -

capacity: 12

length: 7

arrayOffset: 0

limit: 12

position: 5

remaining: 7

content from array: [_, _, a, b, c, _, _, _, _, _, _, _]

- - - - - - - - - - - - - - -

capacity: 12

length: 8

arrayOffset: 0

limit: 12

position: 4

remaining: 8

content from array: [a, b, c, d, c, _, _, _, _, _, _, _]

谢谢您!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值