简体   繁体   English

减去不同长度循环问题的数组

[英]Subtracting arrays of different length loop issue

I am receiving inconsistent results from my function that will subtract elements in arrays of 32 digits of different lengths. 我从函数中收到不一致的结果,该结果将减去长度不同的32位数字数组中的元素。 Rather than starting at the end of both arrays, it is starting at the start of the second array and going to the end. 它不是在两个数组的末尾开始,而是在第二个数组的开始处开始,一直到结尾。 I am slightly confused as to why, because my loop is starting at the end. 我对原因有些困惑,因为我的循环从头开始。 Any help will be appreciated. 任何帮助将不胜感激。

int carry = 0;
    int total = Math.max(tempArray.length, tempArrayTwo.length);
    int[] subArray = new int[total];
    for (int i = differenceArray.length - 1; i >= 0; i--) {
        int first = i < tempArray.length ? tempArray[tempArray.length - i - 1] : 0;
        int second = i >= tempArrayTwo.length ?  0 : tempTwoArray[tempTwoArray.length - i - 1];
        int difference = first - second - carry;
        carry = 0;
        if (difference < 0) {
            difference = difference + 10;
            System.out.println(difference);
            carry = 1;
        } 
        subArray[subArray.length - 1 - i] = difference;

The problem I am having is say array one has a size of 6, 46 total numbers and array two has a size of 3 (24 total numbers) is that the subtraction will start at the start of the 24th character of the first array and the first element of the second array. 我遇到的问题是,数组1的大小为6、46个总数,数组2的大小为3(24个总数)是减法将从第一个数组的第24个字符的开头开始,而第二个数组的第一个元素。

(at the 24th of 46 slot of array One)

00003333 33333333 33333333 array One 00003333 33333333 33333333阵列一

10000000 00000000 00000000 arrayTwo 10000000 00000000 00000000数组2

Instead of starting from 3 of arrayOne and 0 of arrayTwo, it will start at 0 of arrayOne and 1 of arrayTwo. 它不是从arrayOne的3和arrayTwo的0开始,而是从arrayOne的0和arrayTwo的1开始。 Very strange, especially since it sometimes will work accordingly. 非常奇怪,特别是因为有时它会相应地工作。 The extra digits from the larger array are carried down, fyi. 来自较大数组的多余数字将被保留下来。 It's just these particular digits that get screwed up. 只是这些特殊的数字被搞砸了。

Your problem is how you are indexing into your arrays 您的问题是如何索引数组

int carry = 0;
int total = Math.max(tempArray.length, tempArrayTwo.length);
int[] subArray = new int[total];
//you want subArray below, not differenceArray
for (int i = subArray.length - 1; i >= 0; i--) {
    int first = i < tempArray.length ? tempArray[tempArray.length - i - 1] : 0;
    //Line above probably should just be int first = tempArray[i];
    int second = i >= tempArrayTwo.length ?  0 : tempTwoArray[tempTwoArray.length - i - 1];
    //Line above probably should just be int second = tempTwoArray[i];
    int difference = first - second - carry;
    carry = 0;
    if (difference < 0) {
        difference = difference + 10;
        System.out.println(difference);
        carry = 1;
    } 
    subArray[subArray.length - 1 - i] = difference;

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

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