[英]How to convert int array into int
Currently i started reading a book about alogirthms so I'm now trying some very simple algorithms to get comfortable with converting and so on.. In this small class I want to enable a school adding function with carry. 目前,我开始阅读一本有关心律的书,因此,我现在正在尝试一些非常简单的算法,以适应转换等等。
How can i convert the resulting Int Array into an int? 如何将生成的Int数组转换为int? I do not know how to convert them adequate.. 我不知道如何将它们转换足够。
The current result is [7, 8, 3, 7, 9, 0, 5, 6] and I want to concat the numbers into one Integer of (78379056). 当前结果是[7、8、3、7、9、0、5、6],我想将这些数字合并为一个整数(78379056)。 Which possibilities do I have? 我有哪些可能性?
public class Addition {
public int[] addiere(int[] a, int[] b) {
int res = 0;
int[] c = new int[a.length];
for(int i = a.length-1 ; i>=0 ; i--) {
res = a[i]+b[i];
if(res>=10) {
c[i] = oneValue(res);
a[i-1]+=1;
} else c[i]=res;
System.out.println("a -- "+a[i]+" b -- "+b[i]+" c -- "+c[i]);
}
return c;
}
public int oneValue(int t) {
String res;
int val;
res=Integer.toString(t);
res = res.substring(res.length()-1);
val = Integer.parseInt(res);
return val;
}
public static void main(String[] args) {
int[] a = {3,4,6,8,9,1,2,4};
int[] b = {4,2,5,7,8,9,3,2};
Addition add = new Addition();
int[] result;
//returns an array of Integers
System.out.println(Arrays.toString(add.addiere(a, b)));
result = add.addiere(a, b);
//HERE should be a method to convert the result ( Array of Integers ) just into a normal integer
}
}
Given the array 给定数组
int arr[] = { 7, 8, 3, 7, 9, 0, 5, 6 };
you can simply do: 您可以简单地执行以下操作:
long num = Long.parseLong(Arrays.stream(arr)
.mapToObj(String::valueOf)
.collect(Collectors.joining()));
which outputs 哪个输出
78379056 78379056
Explanation: 说明:
mapToObj(...)
we convert each element from an int
to a String
using the valueOf
method. 在mapToObj(...)
我们使用valueOf
方法将每个元素从int
转换为String
。 Collectors.joining()
接下来,我们通过Collectors.joining()
将每个单独的字符串收集到一个字符串中 We use long here just in case the number is too big to be contained in an int . 我们在这里使用long,以防万一该数字太大而不能包含在int中 。
You can either convert the array into a String and use Integer.parseInt()
to get this result or you use a simple loop adding up the numbers multiplied by 10 with their position exponent: 您可以将数组转换为String并使用Integer.parseInt()
获得此结果,也可以使用简单的循环将数字乘以10及其位置指数相加:
int r = 0;
for (int i = 0; i < result.length; i++) {
r += result[i] * Math.pow(10, result.length - i - 1);
}
I would prefer this solution. 我更喜欢这种解决方案。
The result for the array [7, 8, 3, 7, 9, 0, 5, 6]
is 78379056
. 数组[7, 8, 3, 7, 9, 0, 5, 6]
78379056
[7, 8, 3, 7, 9, 0, 5, 6]
为78379056
。
Beside that you should consider using long
instead of int
if you have numbers out of the int range ( 78379056
). 此外,如果您的数字超出int范围( 78379056
),则应考虑使用long
而不是int
。
Edit: Here is a solution with Integer.parseInt()
: 编辑:这是Integer.parseInt()
的解决方案:
StringBuilder builder = new StringBuilder();
for (int i : result) {
builder.append(i);
}
int r = Integer.parseInt(builder.toString());
Alternatively you can take a look at Nicholas K 's answer. 另外,您可以看看Nicholas K的答案。
Eventually, you could multiply each number by a power of 10 and add them together. 最终,您可以将每个数字乘以10的幂并将它们相加。 For example this code will return "1234". 例如,此代码将返回“ 1234”。
int[] array = {1, 2, 3, 4};
int total = 0;
for(int i = 0; i < array.length; i++)
if(array[i] > 9 && array[i] < 0)
throw new IllegalArgumentException("Only use digits");
else
total += array[i] * Math.pow(10, array.length - i - 1);
System.out.println(total);
It works in all cases, except cases with number. 它适用于所有情况,带数字的情况除外。 Make sure you handle the error. 确保处理错误。
(be carrefull to Integer.MAX_VALUE) (对Integer.MAX_VALUE充满信心)
public static void main(String[] args) {
int[] a = {7, 8, 3, 7, 9, 0, 5, 6};
int m = 1;
int r = 0;
for (int i=a.length-1; i>=0; i--) {
r = a[i] * m + r;
m = m * 10;
}
System.out.println(r);
}
prints: 印刷品:
78379056
You can use BigInteger to hold the number which is more than int max size as well as you can avoid NumberFormatException. 您可以使用BigInteger来保存大于int max大小的数字,并且可以避免NumberFormatException。
public static void main(String[] args) {
int[] ary = {2,1,4,7,4,8,3,6,4,7};
StringBuilder numBuilder = new StringBuilder();
for(int num:ary) {
numBuilder.append(num);
}
BigInteger maxInt = BigInteger.valueOf(Integer.MAX_VALUE);
BigInteger finalNum = new BigInteger(numBuilder.toString());
if(finalNum.compareTo(maxInt)>0) {
//number is more the max size
System.out.println("number is more than int max size");
}else {
int result = finalNum.intValueExact();
System.out.println(result);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.