3 回答

TA貢獻2011條經驗 獲得超2個贊
您正在溢出整數的最大大小 2147483647。解決此問題的一種方法是使用 aBigInteger而不是 a int:
BigInteger bigInt = BigInteger.ZERO;
for (int i : ints) {
bigInt = bigInt.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
}

TA貢獻1946條經驗 獲得超3個贊
您可以像這樣執行它:
Integer[] arr = {6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6};
Long val = Long.valueOf(Arrays.stream(arr).map(String::valueOf).collect(Collectors.joining("")));

TA貢獻1854條經驗 獲得超8個贊
public static long convert(int[] arr) {
long res = 0;
for (int digit : arr) {
// negative value is marker of long overflow
if (digit < 0 || res * 10 + digit < 0)
throw new NumberFormatException();
res = res * 10 + digit;
}
return res;
}
這不是一種通用方法,因為Long.MAX_VALUE. 否則,您必須使用長而BigInteger不是長。
public static BigInteger convert(int[] arr) {
// reserve required space for internal array
StringBuilder buf = new StringBuilder(arr.length);
for (int digit : arr)
buf.append(digit);
// create it only once
return new BigInteger(buf.toString());
}
添加回答
舉報