java中的动态数组合并 [英] Dynamic array merge in java

查看:18
本文介绍了java中的动态数组合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的数组.

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

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

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

int firstArray = 2;//输入值int secondArray = 4;//输入值

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

<代码> 121112223334443155566677788823999111222333

循环应该运行到哪个数组长度最大并从两个数组中检索所有值.

如果数组长度改变了,那么输出比例也应该改变

String[] arr1 = { "1", "2", "3", "4", "5", "6", "7", "8" };String[] arr2 = { "111", "222", "333", "444", "555" };int firstArray = 3;//输入值int secondArray = 2;//输入值

输出:

123111222456333444781555111

所以条件是输出应该包含来自两个数组的所有值,直到数组的最大长度和输出应该以第二个比率(第二个输入值 - secondArray)完成.

提前致谢.

String[] arr = mergeArrays(arr1, arr2, 2, 3);System.out.println("比例2:3");for (String str : arr) {System.out.println(str);}私有静态字符串[]合并数组(字符串[] arr1,字符串[] arr2,int firstArray,int secondArray){最终字符串[] ret = 新字符串[arr1.length + arr2.length];for (int j = 0, k = 0; j 

我尝试了这个解决方案

但我无法正确输出

解决方案

我使用 List 而不是数组,因为我认为你不能真正预测结果数组的长度.

>

public static 列表合并(列表 l1,列表 l2,int r1,int r2){列表结果 = 新的 ArrayList();int index1 = 0;int index2 = 0;while (index1 

测试代码:

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]

I have two array like this.

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

I want to merge these two array using combination of index value.

My input will be two integer value (2:3 ratio), like this

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

After merge all value will be stored in single list. Now i need output like this.

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

loop should run till which array length is big and retrive all values from both array.

if array length got changed then output ratio also should change

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

output :

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

so condition is output should contain all values from both array and till maximum length of array and output should completed with second ratio(second input value - secondArray).

thanks in advance.

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

I tried from this solution

but i cant able to bring correct out put

解决方案

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]

这篇关于java中的动态数组合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆