[추가] 이것은 ArrayIndexOutOfBoundsException에 관한 문제가 아닙니다. 순전히 알림 계산 알고리즘에 관한 것이므로 복제 된 것으로 표시하지 마십시오.ArrayIndexOutOfBoundsException을 일으키는 mod 계산의 수학 공식을 이해하는 방법은 무엇입니까?
전화 여기서, ArrayIndexOutOfBoundsException을, 로그이다
java.lang.ArrayIndexOutOfBoundsException : 길이 = 36; index = 120 at 에있는 java.lang.IntegralToString.convertInt (IntegralToString.java:234) 에있는 java.lang.IntegralToString.appendInt (IntegralToString.java:173) java.lang.StringBuilder.append (StringBuilder.java:139))는 예외
발생
에서 com.android.internal.telephony.ServiceStateTracker.onSignalStrengthResult (ServiceStateTracker.java:958) 에서 android.telephony.SignalStrength.toString (SignalStrength.java:1123)에서
buf [- 커서] = DIGITS [r];
질문은 (* (0x51EB851FL를 I) >>> 37) 코드 등
INT의 Q = (int)를 이해하는 방법이며;
및
INT의 Q = (0xCCCD * I) >>> 19;
[삭제] 왜 int q = i/10; int r = i - 10 * q;
이유 int q = (0xCCCD * i) >>> 19; int q = i/10과 같습니다.
올바른 알고리즘 인 경우 위의 알고리즘에 대한 설명에 따라 r은 120 일 수 있습니다. 아래
는 관련 코드이다 : 숫자의 시작private static final char[] TENS = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9'
};
/** Ones [i] contains the tens digit of the number i, 0 <= i <= 99. */
private static final char[] ONES = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
};
/**
* The digits for every supported radix.
*/
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
};
/**
* Returns the string representation of i and leaves sb alone if sb is null.
* Returns null and appends the string representation of i to sb if sb is non-null.
*/
private static String convertInt(AbstractStringBuilder sb, int i) {
boolean negative = false;
String quickResult = null;
if (i < 0) {
negative = true;
i = -i;
if (i < 100) {
if (i < 0) {
// If -n is still negative, n is Integer.MIN_VALUE
quickResult = "-2147483648";
} else {
quickResult = SMALL_NEGATIVE_VALUES[i];
if (quickResult == null) {
SMALL_NEGATIVE_VALUES[i] = quickResult =
i < 10 ? stringOf('-', ONES[i]) : stringOf('-', TENS[i], ONES[i]);
}
}
}
} else {
if (i < 100) {
quickResult = SMALL_NONNEGATIVE_VALUES[i];
if (quickResult == null) {
SMALL_NONNEGATIVE_VALUES[i] = quickResult =
i < 10 ? stringOf(ONES[i]) : stringOf(TENS[i], ONES[i]);
}
}
}
if (quickResult != null) {
if (sb != null) {
sb.append0(quickResult);
return null;
}
return quickResult;
}
int bufLen = 11; // Max number of chars in result
char[] buf = (sb != null) ? BUFFER.get() : new char[bufLen];
int cursor = bufLen;
// Calculate digits two-at-a-time till remaining digits fit in 16 bits
while (i >= (1 << 16)) {
// Compute q = n/100 and r = n % 100 as per "Hacker's Delight" 10-8
int q = (int) ((0x51EB851FL * i) >>> 37);
int r = i - 100*q;
buf[--cursor] = ONES[r];
buf[--cursor] = TENS[r];
i = q;
}
// Calculate remaining digits one-at-a-time for performance
while (i != 0) {
// Compute q = n/10 and r = n % 10 as per "Hacker's Delight" 10-8
int q = (0xCCCD * i) >>> 19;
int r = i - 10*q;
buf[--cursor] = DIGITS[r];
i = q;
}
if (negative) {
buf[--cursor] = '-';
}
if (sb != null) {
sb.append0(buf, cursor, bufLen - cursor);
return null;
} else {
return new String(cursor, bufLen - cursor, buf);
}
}
기타 질문 : ** int q = i/10이 아닌 이유는 무엇입니까? int r = i - 10 * q; **, 비트 시프트 연산은 대개 (?) 빠릅니다. 그러나 컴파일러는 비트 시프트에서 곱셈을 수행해야하는지 아니면 다른 작업을해야하는지 더 잘 알고 있습니다. 따라서 컴파일러에서 어셈블리를 구현하는 것이 가장 좋습니다. 따라서 곱셈을 사용해야하는 경우 곱셈을 사용하려면 나눗셈을 사용하고 나누기가 필요하며 비트 쉬프트는 수행하지 말아야합니다. – rafid059
정말 질문이 없습니다. 당신은 ** 당신의 ** mod 계산이 그 예외를 이끌어내는 이유를 묻고 있습니까? 아니면 ** 두 개의 완전히 독립적 인 것들을 묻고 있습니까? – GhostCat
나는 이런 종류의 수식을 사용하는 이유를 이해할 수있다 "int q = (0xCCCD * i) >>> 19;" 그것은 직관적이고 어렵고 그것이하는 일과 그것이 올바르게 수행되는지 이해하는 것이 어렵습니다. – Saint