将 ArrayList 折叠为单个整数

Make Folding an ArrayList to single integer

我正在为 java 开发一个代码,以使用示例

折叠数组中的元素

A[0] = 2, A[1] = 7, A[2] = 9 , A[3] = 7

然后按照这个格式折叠

A[0] = (A[0] + A[3]) mod 10 = 9

A[1] = (A[1] + A[2]) mod 10 = 6

再对折一次

A[0] = (A[0] + A[1]) mod 10 = 5

以下是不完整的代码:

import java.util.ArrayList;
import java.util.List;
import java.lang.Math;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        int[] A = {
            2,
            7,
            9,
            7
        };

        //To define a new Array later
        List<Integer> intList = new ArrayList<Integer>();

        //To print the ArrayList A
        for (int x = 0; x < A.length; x++) {
            System.out.println("A[" + x + "]= " + A[x]);
        }

        //Function to fold the ArrayList into half
        int res = 0;
        int result = 0;

        //if A.length is Even
        if (A.length % 2 == 0) {
            for (int i = 0; i < A.length / 2; i++) {
                res = A[i] + A[A.length - 1 - i];
                result = res % 10;
                intList.add(result);
            }
        }

        //if A.length is Odd
        else {
            for (int i = 0; i < A.length / 2; i++) {
                res = A[i] + A[A.length - 1 - i];
                result = res % 10;
                intList.add(result);
            }
            result = A[A.length / 2];
            intList.add(result);
        }

        //add the int to New ArrayList
        Integer[] intArray = new Integer[intList.size()];
        intArray = intList.toArray(intArray);
        System.out.println("\nNew Array ");

        for (Integer s: intArray) {
            System.out.println(s);
        }

    }
}

编译结果

A[0]= 2
A[1]= 7
A[2]= 9
A[3]= 7

New Array
9
6

我找不到一种有效的方法来继续循环该函数,因此代码将在实现单个 Integer 时停止折叠。

我的问题是,是否有任何有效的方法来循环该过程以便稍后它可以处理更大的数组元素?

请提供逻辑或代码,以便我可以继续我的代码。

非常感谢

您可以将折叠算法放入一个单独的方法中,returns 您将折叠数组并调用此方法,直到获得单个整数,例如:

import java.util.ArrayList;
import java.util.List;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        Integer[] A = {
            2,
            7,
            9,
            7
        };

        while (A.length > 1) {
            A = fold(A);
        }

        System.out.println("A[0]= " + A[0]);

    }

    private static Integer[] fold(Integer[] A) {
        List<Integer> intList = new ArrayList<>();
        //To print the ArrayList A
        for (int x = 0; x < A.length; x++) {
            System.out.println("A[" + x + "]= " + A[x]);
        }

        //Loop to fold the ArrayList into half
        for (int i = 0; i < A.length / 2; i++) {
            int res = A[i] + A[A.length - 1 - i];
            int result = res % 10;
            intList.add(result);
        }
        
        //if A.length is odd
        if (A.length % 2 != 0) {
            intList.add(A[A.length / 2]);
        }
        System.out.println("\n");
        
        return intList.toArray(new Integer[intList.size()]);
    }
}

你也可以使用递归的方法,即折叠方法会调用自己进一步折叠,直到达到一个整数。

import java.util.ArrayList;
import java.util.List;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        final Integer[] A = {
            2,
            7,
            9,
            7
        };

        fold(A);
    }

    private static void fold(Integer[] A) {
        if (A.length > 0) {
            List<Integer> intList = new ArrayList<>();
            //To print the ArrayList A
            for (int x = 0; x < A.length; x++) {
                System.out.println("A[" + x + "]= " + A[x]);
            }

            //Loop to fold the ArrayList into half
            for (int i = 0; i < A.length / 2; i++) {
                int res = A[i] + A[A.length - 1 - i];
                int result = res % 10;
                intList.add(result);
            }

            //if A.length is odd
            if (A.length > 1 && A.length % 2 != 0) {
                intList.add(A[A.length / 2]);
            }
            System.out.println("\n");
        
            fold(intList.toArray(new Integer[intList.size()]));
        }
    }
}

在同一个数组中折叠:

public static void main(String[] args) throws Exception {
    int[] A = {2,7,9,7};
    System.out.println("Input: " + Arrays.toString(A));

    int length = A.length;
    int mid = mid(length);
    while(mid > 0) {  //fold till only one left to fold
        int i = 0;
        for (; i <mid ; i++) { //single fold
            int end = length-i-1;
            if(i!=end) {
                A[i] = (A[i] + A[end])%10;
            }
        }
        System.out.println(Arrays.toString(Arrays.copyOf(A, mid)));
        length = mid;
        mid = mid(i);
    }
    System.out.println("Output: " + A[0]);
}
static int mid(int length) {
    if(length == 1) return 0;
    return (int)Math.ceil(length/2.0);
}

输出:

Input: [2, 7, 9, 7]
[9, 6]
[5]
Output: 5

对于奇数:

Input: [2, 7, 3, 9, 7]
[9, 6, 3]
[2, 6]
[8]
Output: 8