将整数数组组合为单个整数变量的方法? [英] Way to combine integer array to a single integer variable?

查看:80
本文介绍了将整数数组组合为单个整数变量的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过余数法查找数字的各个数字后,数字将保存在数组中。
我现在想要的是获取此数组的各个元素,并为其制作一个整数值。

After finding the individual digits of a number by remainder procedure, the numbers are saved in an array. What I want now is to take the individual elements of this array and make a single integer value of it.

例如。

int a = 4400

使用递归(4400/10)保存在数组中的数字:
让数组成为arr []

digits saved in array by using recursion (4400/10) : let array be arr[]

arr[0]=0;
arr[1]=0;
arr[2]=4;
arr[3]=4;

最终值:

int b= 4400 (by combining elements of array)

所以我想知道是否可以将数组元素组合为单个整数值?

So I want to know if there is any way to combine the array elements to a single integer value ?

推荐答案

只需将数字相乘并相加即可:

Just multiply and add the digits:

int result = 1000 * arr[3] + 100 * arr[2] + 10 * arr[1] + arr[0];

或者,如果您需要使用它来处理任意长度的数组(不超过位数) Integer.MAX_VALUE ):

Or, if you need it to work for any length of array (up to the number of digits in Integer.MAX_VALUE):

int result = 0;
for (int i = arr.length - 1; i >= 0; --i) {
  result = 10*result + arr[i];
}

这篇关于将整数数组组合为单个整数变量的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆