gpt4 book ai didi

java - 整数迭代帮助 java

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:17 25 4
gpt4 key购买 nike

我目前遇到了有关将整数数组合并为整数的问题。

我在 Way to combine integer array to a single integer variable? 上研究了其他几种实现它们的方法。 ,但我仍然不明白为什么我会遇到错误。

我的目标是:

[6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6]

进入

62338777016

目前,当给定较小的整数数组时,它可以工作,例如:

[1, 3, 4, 4]
-> 1344

一旦元素数量达到 10,它就会开始崩溃。有人有可行的解决方案吗?

最佳答案

您溢出了整数的最大大小 2147483647。解决此问题的一种方法是使用 BigInteger而不是 int:

BigInteger bigInt = BigInteger.ZERO;
for (int i : ints) {
bigInt = bigInt.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
}

关于java - 整数迭代帮助 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700388/

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