我确定有人知道如何使用Unsafe类获取数组的地址。我查看了相关帖子,但实际上并没有答案。
static Unsafe _u;
static int Atoi8(char[] s, int i0, int cb) {
int n = 0;
// loop to process the valid number chars.
int baseOffset = _u.arrayBaseOffset(s.getClass()); <-- returns 16 oops!
for (int i=i0 ; i<i0+cb ; ++i) {
char ch = _u.getChar(baseOffset + i);
n = n*10 + ((ch <= '0') ? 0 : ch - '0');
}
return n;
}
请:不要讨论不安全代码,预优化,使用其他语言或性能与可维护性的危险!