在Java中使用比例明智合并两个数组 [英] Merge two array using ratio wise in java

查看:261
本文介绍了在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.

我的投入会有两个整数(2:3比例),这样

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

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.

在调用循环输入值时,所以第二次。(3:4的比例)应该是这样的。

So second time when calling the for loop input value (3:4 ratio) should be like this.

int firstArray = 3; 
int secondArray = 4;

第二次输出:

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. I couldn't able to do proper functionality for this. Can anybody give suggestion to build a proper functionality.

下面是我的code是我的尝试。

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

}

先谢谢了。

推荐答案

您可以试试:

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

下面是如何称呼它:

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

这篇关于在Java中使用比例明智合并两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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