gpt4 book ai didi

java - CharBuffer equals() 方法究竟是如何工作的?

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:00:26 24 4
gpt4 key购买 nike

我无法理解 CharBuffer equals() 方法功能的具体细节。

我不明白这个“独立于他们的起始位置考虑”的短语:

Two char buffers are equal if, and only if,

They have the same element type,

They have the same number of remaining elements, and

The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

我研究了这些很好的例子 - more examples ,但我不明白这个想法。

谁能用不同的词和最少的有见地的示例代码来解释?

特别是我觉得这很奇怪:

CharBuffer cb1 = CharBuffer.allocate(10);
cb1.put('a');
cb1.put('b');
//cb1.rewind();
System.out.println(cb1);


CharBuffer cb2 = CharBuffer.allocate(10);
cb2.put(4,'a');
cb2.put(5,'b');
//cb2.rewind();
System.out.println(cb2);

// false, uncommenting rewind() - also false
// but shall be true - "considered independently of starting positions" ?
System.out.println(cb1.equals(cb2));

最佳答案

The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

  1. 只看剩下的元素,
  2. 不考虑他们从哪里开始,
  3. 一个缓冲区的每个元素必须等于另一个缓冲区中的相应元素

在您的示例中,重要的是pointwise equal部分:

      0 1 2 3 4 5 6 7 8 9
cb1 = a b 0 0 0 0 0 0 0 0
cb1 = 0 0 0 0 a b 0 0 0 0

如您所见,逐点比较 char 缓冲区的元素时,它们不匹配。

关于java - CharBuffer equals() 方法究竟是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54022991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com