繁体   English   中英

如何将int数组转换为int

[英]How to convert int array into int

目前,我开始阅读一本有关心律的书,因此,我现在正在尝试一些非常简单的算法,以适应转换等等。

如何将生成的Int数组转换为int? 我不知道如何将它们转换足够。

当前结果是[7、8、3、7、9、0、5、6],我想将这些数字合并为一个整数(78379056)。 我有哪些可能性?

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
    }

}

给定数组

int arr[] = { 7, 8, 3, 7, 9, 0, 5, 6 };

您可以简单地执行以下操作:

long num = Long.parseLong(Arrays.stream(arr)
                                .mapToObj(String::valueOf)
                                .collect(Collectors.joining()));

哪个输出

78379056


说明:

  1. mapToObj(...)我们使用valueOf方法将每个元素从int转换为String
  2. 接下来,我们通过Collectors.joining()将每个单独的字符串收集到一个字符串中
  3. 现在,我们将此 String转换为long。 您可以在此处从文档中阅读有关流的更多信息

我们在这里使用long,以防万一该数字太大而不能包含在int中

您可以将数组转换为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);
}

我更喜欢这种解决方案。

数组[7, 8, 3, 7, 9, 0, 5, 6] 78379056 [7, 8, 3, 7, 9, 0, 5, 6]78379056

此外,如果您的数字超出int范围( 78379056 ),则应考虑使用long而不是int

编辑:这是Integer.parseInt()的解决方案:

StringBuilder builder = new StringBuilder();
for (int i : result) {
    builder.append(i);
}
int r = Integer.parseInt(builder.toString());

另外,您可以看看Nicholas K的答案。

最终,您可以将每个数字乘以10的幂并将它们相加。 例如,此代码将返回“ 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);

它适用于所有情况,带数字的情况除外。 确保处理错误。

(对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);
    }

印刷品:

78379056

您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM