简体   繁体   English

如何在Java中多重播放整数数组

[英]How to multiplay an array of Integers in Java

I have two arrays of integers and I want to multiply them together like the following: 我有两个整数数组,我想像下面这样将它们相乘:

int[] arr1 = {6, 1}; // This represents the number 16

int[] arr2 = {4, 2}; // This represents the number 24

I want to store them in a new array so that the product appears as: 我想将它们存储在新数组中,以便产品显示为:

int[] productArray = {4, 8, 3};

I get how to do it with multiplying numbers like 2 x 24 since I can just push those into values into the product array. 我知道如何用2 x 24之类的数字进行乘法运算,因为我可以将这些数值推入乘积数组中。 But when it comes to expanding beyond a single digit I am lost. 但是当涉及到超过一位数的扩展时,我迷失了。

Why do you need to do this? 为什么需要这样做? Regardless, here is an example: 无论如何,这是一个示例:

int total1; // to find what the array represents in #
for (int c = 0; c < arr1.length() - 1; c++) {
 total1 += arr1[c] * Math.pow(10, (c+1)); // takes the number and adds it to the decimal number it should be
}
int total2;
for (int c = 0; c < arr2.length() - 1; c++) {
 total1 += arr2[c] * Math.pow(10, (c+1));
}
String s = Integer.toString(total2*total1);
for (int c = s.length()-1; c >= 0; c--) { // adds int to array backwards
  productArray.add(Integer.parseInt(s.atIndex(c)));
}

Notes: I have not bug tested this or run it through the JVM. 注意:我没有对此进行错误测试或通过JVM运行它。 Java isn't my usual programming language so I may have made a few mistakes. Java不是我常用的编程语言,因此我可能犯了一些错误。 the "productArray" needs to be an ArrayList<> instead. “ productArray”需要改为ArrayList <>。 I suspect that Integer.parseInt() is only for strings, so you may have to search for the char version of the function. 我怀疑Integer.parseInt()仅用于字符串,因此您可能必须搜索该函数的char版本。 Also, you need include Math... 另外,您需要包括数学...

int arr1num, arr2num, product;
multiplier = 1;
for (i = arr1.size();i>0;i--){
arr1num = arr1num + (arr1[i] * multiplier);
multiplier = multiplier * 10;
}

--do this also for the second array -对第二个数组也这样做

-- now we have arr1num and arr2num as the numbers of both arrays and just get the product -现在我们将arr1num和arr2num作为两个数组的数字,然后得到乘积

product = arr1num * arr2num;

-- now store it in an array -现在将其存储在数组中

int divisor = 10;
int number;
for(i=0;i<int.length();i++){

number = product % divisor;
productArray.add(number);
divisor = divisor * 10;
}

You could use this (although it's not the best algorithm you could use to do this): 您可以使用此方法(尽管它不是执行此操作的最佳算法):

public static int[] multiplyArrays(int[] a, int[] b){
    //turns arrays into integers
    String num1 = "";
    for (int i = a.length - 1; i > -1; i--){
        num1 += a[i];
    }

    String num2 = "";
    for (int i = b.length - 1; i > -1; i--){
        num2 += b[i];
    }

    //does calculation
    char[] answer = Integer.toString(Integer.parseInt(num1) * Integer.parseInt(num2)).toCharArray();

    //converts char array into int array
    int[] answer2 = new int[answer.length];

    for (int i = 0; i < answer.length; i++){
        answer2[answer.length - i - 1] = Integer.parseInt(String.valueOf(answer[i]));
    }

    return answer2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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