我为字符串"a“创建了CollationKey,然后使用toByteArray()方法将CollationKey转换为位序列。之后,我使用Arrays.toString()来显示这个byte[]数组,并得到一个我不明白的输出。我想我会用位表示字符串。如何解释输出?谢谢
package myPackage9;
import java.text.CollationKey;
import java.text.*;
import java.lang.*;
import java.util.Arrays;
public class collatorClass {
public static void main(String[] args) {
Collator myCollator = Collator.getInstance();
CollationKey[] a = new CollationKey[1];
a[0] = myCollator.getCollationKey("a");
byte[] bytes= a[0].toByteArray();
System.out.println(Arrays.toString(bytes));
}
}output: [0, 83, 0, 0, 0, 1, 0, 0, 0, 1]发布于 2021-03-22 18:49:15
CollationKey是一个抽象类。很可能您的具体类型是RuleBasedCollationKey。首先,让我们看一下方法的JavaDoc:
将CollationKey转换为位序列。如果两个CollationKeys可以合法地进行比较,那么一个可以比较每个键的字节数组,以获得相同的结果。字节数组首先组织最重要的字节。
显然,"a“的排序规则键不像字符串"a”那样用相同的字节表示,这并不令人惊讶。
下一步是查看其来源,了解它到底返回了什么:
public byte[] toByteArray() {
char[] src = key.toCharArray();
byte[] dest = new byte[ 2*src.length ];
int j = 0;
for( int i=0; i<src.length; i++ ) {
dest[j++] = (byte)(src[i] >>> 8);
dest[j++] = (byte)(src[i] & 0x00ff);
}
return dest;
}key是什么?它作为第二个构造函数参数传入。构造函数在RuleBasedCollator#getCollationKey中调用。源非常复杂,但是该方法的JavaDoc声明:
将字符串转换为可与CollationKey.compareTo比较的一系列字符。这覆盖了java.text.Collator.getCollationKey。它可以在子类中被覆盖。
通过查看该方法的内联代码注释,将进一步解释:
// The basic algorithm here is to find all of the collation elements for each
// character in the source string, convert them to a char representation,
// and put them into the collation key. But it's trickier than that.
// Each collation element in a string has three components: primary (A vs B),
// secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference
// at the end of a string takes precedence over a secondary or tertiary
// difference earlier in the string.
//
// To account for this, we put all of the primary orders at the beginning of the
// string, followed by the secondary and tertiary orders, separated by nulls.接着是一个假设的例子:
// Here's a hypothetical example, with the collation element represented as
// a three-digit number, one digit for primary, one for secondary, etc.
//
// String: A a B \u00e9 <--(e-acute)
// Collation Elements: 101 100 201 510
//
// Collation Key: 1125<null>0001<null>1010因此,假设CollationKey的toByteArray()方法将返回与String的toByteArray()方法相同的结果,这是完全错误的。
"a".toByteArray()与Collator.getInstance().getCollationKey("a").toByteArray()不一样。如果是的话,我们就不需要整理钥匙了,是吗?
https://stackoverflow.com/questions/66751480
复制相似问题