动态数组在java中合并

时间:2015-01-09 12:46:12

标签: java arrays list sorting linked-list

我有两个像这样的数组。

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

我想使用索引值的组合来合并这两个数组。

我的输入将是两个整数值(2:3比例),就像这样

int firstArray = 2;    //input value
int secondArray = 4;   //input value

合并后,所有值都将存储在单个列表中。现在我需要像这样的输出。

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

循环应该运行,直到哪个数组长度很大并且从两个数组中检索所有值。

如果数组长度发生变化,那么输出比也应该改变

String[] arr1 = { "1", "2", "3", "4", "5", "6", "7", "8" };
String[] arr2 = { "111", "222", "333", "444", "555" };

int firstArray = 3;    //input value
int secondArray = 2;   //input value

输出:

1
2
3
111
222
4
5
6
333
444
7
8
1
555
111

因此输出的条件应该包含数组中的所有值,直到数组的最大长度和输出应以第二比率(第二个输入值 - secondArray)完成。

提前感谢。

String[] arr = mergeArrays(arr1, arr2, 2, 3);

        System.out.println("Ratio 2:3");

        for (String str : arr) {
            System.out.println(str);
        }

private static String[] mergeArrays(String[] arr1, String[] arr2, int firstArray, int secondArray) {
        final String[] ret = new String[arr1.length + arr2.length];

        for (int j = 0, k = 0; j < arr1.length || k < arr2.length;) {
            if (j < arr1.length) {
                do {
                    ret[j + k] = arr1[j];
                    j++;
                } while (j < arr1.length && (j % firstArray != 0 || k == arr2.length));
            }
            if (k < arr2.length) {
                do {
                    ret[j + k] = arr2[k];
                    k++;
                } while (k < arr2.length && (k % secondArray != 0 || j == arr1.length));
            }
        }

        return ret;
    }

我试过这个solution

但我无法带来正确的输出

2 个答案:

答案 0 :(得分:3)

这应该有效:

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;
    }
}

如果您想在视频上生成一个新的打印结果数组,只需声明ArrayList并使用add()方法向其中添加元素:

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

如果您想创建一个功能:

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;
}

如果您不想退回ArrayList,只需将其转换为int[]

答案 1 :(得分:1)

我使用List代替数组,因为我不认为你可以真正预测结果数组的长度。

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;
}

测试代码:

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));

输出:

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