简体   繁体   English

在Java中使用比率明智合并两个数组

[英]Merge two array using ratio wise in java

I have two array like this. 我有两个这样的数组。

String[] arr1 = {"11","22","33","44","55","66","77"};
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 我的输入将是两个整数值(比率为2:3),像这样

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

After merge all value will be stored in single list. 合并后,所有值将存储在单个列表中。 Now i need output like this. 现在我需要这样的输出。

11
22
111
222
333
33
44
444
555
666
55
66
777
888
999
77

Input value should increase next time this loop execute on next time. 下次执行该循环时,输入值应增加。

So second time when calling the for loop input value (3:4 ratio) should be like this. 因此,第二次调用for循环输入值(3:4的比例)时应像这样。

int firstArray = 3; 
int secondArray = 4;

Second Time Output: 第二次输出:

11
22
33
111
222
333
444
44
55
66
555
666
777
888
77
999

I tried using for loop and circular linked list. 我尝试使用for循环和循环链表。 I couldn't able to do proper functionality for this. 我无法为此执行适当的功能。 Can anybody give suggestion to build a proper functionality. 任何人都可以提出建议以构建适当的功能。

Here is my code what i tried. 这是我尝试的代码。

String[] arr1 = {"11","22","33","44","55","66","77"};
String[] arr2 = {"111","222","333","444","555","666","777","888","999"};

int f = 2;

for(int i=0;i<arr1.length;i++){

    if(i == f){             
        f = f+f;    

        int s = 3;      
        for(int j=0;j<arr2.length ;j++){

            if(j == s){                             
                s = s+s;
            }

            if(j < s)
                System.out.println(arr2[j]);                        

        }

    }

    if(i < f)
        System.out.println(arr1[i]);

}

Thanks in advance. 提前致谢。

You can try that: 您可以尝试:

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;) {
        while (j < arr1.length) {
            ret[j + k] = arr1[j];
            if (++j % firstArray == 0)
                break;
        }
        while (k < arr2.length) {
            ret[j + k] = arr2[k];
            if (++k % secondArray == 0)
                break;
        }
    }

    return ret;
}

Here is how to call it: 调用方法如下:

public static void main(String[] args) {
    String[] arr1 = { "11", "22", "33", "44", "55", "66", "77" };
    String[] arr2 = { "111", "222", "333", "444", "555", "666", "777", "888", "999" };

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

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

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

    arr = mergeArrays(arr1, arr2, 3, 4);

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

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

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

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