Dynamic array merge in java [closed]

那年仲夏 提交于 2019-12-04 21:56:43

I used List instead of arrays, because I don't think you can really predict the length of the resulting array.

public static <T> List<T> merge(List<T> l1, List<T> l2, int r1, int r2) {
    List<T> result = new ArrayList<T>();

    int index1 = 0;
    int index2 = 0;

    while (index1 < l1.size() || index2 < l2.size()) {
        for (int i = 0; i < r1; ++i)
            result.add(l1.get((index1 + i) % l1.size()));
        index1 += r1;
        if (index2 < l2.size()) {
            for (int i = 0; i < r2; ++i)
                result.add(l2.get((index2 + i) % l2.size()));
            index2 += r2;
        }

    }
    return result;
}

Test code:

String[] arr1 = { "1", "2", "3" };
String[] arr2 = { "111", "222", "333", "444", "555", "666", "777",
            "888", "999" };

System.out.println(merge(Arrays.asList(arr1), Arrays.asList(arr2), 2, 4));

Output:

[1, 2, 111, 222, 333, 444, 3, 1, 555, 666, 777, 888, 2, 3, 999, 111, 222, 333]

This should work:

public static void main(String args[]) throws IOException {
    int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8};
    int[] array2 = {11, 22, 33};

    int firstArray = 3;
    int secondArray = 5;

    for (int a1 = 0, a2 = 0; ;) {
        for (int i = 0; i < firstArray; i++)
            System.out.println(array1[a1++ % array1.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
        for (int i = 0; i < secondArray; i++)
            System.out.println(array2[a2++ % array2.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
    }
}

If you want to generate a new array instaed of print result on video, simply declare an ArrayList and add elements to it using the add() method:

public static void main(String args[]) throws IOException {
    int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8};
    int[] array2 = {11, 22, 33};

    int firstArray = 3;
    int secondArray = 5;

    ArrayList<Integer> output = new ArrayList();

    for (int a1 = 0, a2 = 0; ;) {
        for (int i = 0; i < firstArray; i++)
            output.add(array1[a1++ % array1.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
        for (int i = 0; i < secondArray; i++)
            output.add(array2[a2++ % array2.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
    }

    for (int i = 0; i < output.size(); i++)
        System.out.println(output.get(i));
}

If you want to create a function:

public ArrayList<Integer> merge(int[] array1, int[] array2, int firstArray, int secondArray) {

    ArrayList<Integer> output = new ArrayList();

    for (int a1 = 0, a2 = 0; ;) {
        for (int i = 0; i < firstArray; i++)
            output.add(array1[a1++ % array1.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
        for (int i = 0; i < secondArray; i++)
            output.add(array2[a2++ % array2.length]);
        if (a1 >= array1.length && a2 >= array2.length)
            break;
    }

    return output;
}

If you don't want to return an ArrayList, simply convert it to an int[]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!