使用两个不同的对象动态填充列表

时间:2014-10-25 02:56:59

标签: java logic

我有2个列表(list1和list2),我想有效地将​​它们合并到一个列表(list3)。

对于每个x list1对象插入list3,我想将list2的y对象插入list3。

list3的最终结果看起来像{x,x,x,y,y,x,x,x,y,y,x,x,x}

我的尝试:这有效,但对我来说似乎很笨拙。任何帮助将不胜感激

for (int j = 1; f < list1.size(); j++) {
    if ((j % 4) != 0) {
        list3.add(this.list1.get(f));
        f += 1;
    } else {
        if (b < count) {
            for (int k = tracker; (k % 3) != 0; k++) {
                if (b < count) {
                    list3.add(this.list2.get(b));
                    b += 1;
                    tracker += 1;
                }
            }
            tracker += 1;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

例如:

boolean flag = true;
int counter = 0;
Iterator<Object> iterator1 = list1.iterator();
Iterator<Object> iterator2 = list2.iterator();
while ( iterator1.hasNext() || iterator2.hasNext() ) {
    if (flag) {
        if (counter < 3 && iterator1.hasNext()) {
            list3.add(iterator1.next());
            counter++;
        } else {
            flag = !flag;
            counter = 0;
        }
    } else {
        if (counter < 2 && iterator2.hasNext()) {
            list3.add(iterator2.next());
            counter++;
        } else {
            flag = !flag;
            counter = 0;
        }
    }
}

答案 1 :(得分:0)

此解决方案适用于数组,但可以作为其他列表的参考。

int list1[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int list2[] = { 3, 3, 3, 3, 3, 3 };
int list3[] = new int[100];
int i = 1, j, k = 0, n = 0, m = 0;
while (k < list1.length + list2.length) {
    if (i++ % 5 != 0 && n < list1.length) {
        list3[k++] = list1[n++];
    } else {
        i = 1;
        j = 1;
        while (j++ % 3 != 0 && m < list2.length)
            list3[k++] = list2[m++];
    }
}
for (i = 0; i < k; i++)
    System.out.print(list3[i] + " ");

答案 2 :(得分:0)

解决新要求:

    int list1[] = {1,1,1,1,1,1,1,1,1,1,1,1};
    int list2[] = {2,2,2,2,2,2,2,2};
    int list3[] = new int[100];
    int list4[] = {4,4,4,4,4,4,4,4};
    int list5[] = new int[100];
    int i=1,j,k=0,n=0,m=0;int h=0;
    while (k < list1.length+list2.length){      
        if (i++%5 !=0 && n<list1.length) {
            list3[k++]=list1[n++];
        }
        else  {
            if (n==list1.length && m<list2.length) {
                System.out.println("\nEnd of list1 and list2 even has " + m + " elements");
                i=1;j=1;n=0;
                int nl2=list2.length-m; //new length of list2
                while (h < list4.length+nl2){      
                    if (i++%5 !=0 && n<list4.length) {
                        list5[h++]=list4[n++];
                    }
                    else  {                
                        i=1;j=1;
                        while (j++%3 !=0 && m<list2.length) list5[h++]=list2[m++];                
                    }                                      
                }   
                break;
            }
            i=1;j=1;
            while (j++%3 !=0 && m<list2.length) list3[k++]=list2[m++];                
        }                                      
    }

    System.out.println("\nList 3:");
    for (i=0;i<k;i++)
        System.out.print(list3[i]+" ");  

    System.out.println("\n\nList 5:");
    for (i=0;i<h;i++) 
        System.out.print(list5[i]+" ");