2的ArrayList之间交替。 [英] Alternating between two arraylists.

查看:197
本文介绍了2的ArrayList之间交替。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦这个东西跑。我不知道,如果是在正确的轨道上我有什么至今。我不太清楚其中是给我一个出界失误的。

下面是说明:

写一个方法叫做交错接受A1和A2作为参数整数两个的ArrayList并插入A2在交替的指标要素变为a1。如果清单是不相等的长度,在较长的列表的其余元件以a1的端离开。例如,如果A1店[10,20,30]和a2店[4,5,6,7,8],交织的呼叫(A1,A2);应该改变a1至存储[10,4,20日,5,30,6,7,8]。如果A1已经存储[10,20,30,40,50]和a2已存储的[6,7,8],交织的呼叫(A1,A2);会改变a1至存储[10,6,20,7天,30 8,40,50]。

 私有静态无效交错(ArrayList的<整数GT; A1,
        ArrayList的<整数GT; a2) {    INT I = a1.size();
    INT J = a2.size();    如果(ⅰ&所述; j)条{// a1为比A2较短
        对于(INT K = 0; K< a1.size(); K ++){//ķ之前所经过A1尺寸
            a1.add(K + 1,a2.get(K));
        }        为(中间体L = a1.size(); L&下; a2.size()为:L ++){
            a1.add(a1.size(),a2.get(1));
        }    }否则如果(ⅰ> j)条{// a1为比A2长
        对于(INT K = 1; K< a2.size(); K ++){
            a1.add(K + 1,a2.get(K));
        }    }其他{//他们是平等的长度
        对于(INT K = 1; K< a2.size(); K ++){
            a1.add(K + 1,a2.get(K));
        }
    }
}


解决方案

这应该工作

 私有静态无效交错(ArrayList的<整数GT; A1,ArrayList的<整数GT; A2){
    INT I = -1;
    对于(整数ELEM:A2){
        如果(ⅰ&下; a1.size() - 1){
            I + = 2;
        }其他{
            我+ = 1;
        }
        a1.add(I,ELEM);
    }
}公共静态无效的主要(字串[] args)抛出异常{    ArrayList的<整数GT; A1 =新的ArrayList&所述;>(Arrays.asList(10,20,30));
    ArrayList的<整数GT; A2 =新的ArrayList&所述;>(Arrays.asList(4,5,6,7,8));    交织(A1,A2);
    的System.out.println(A1);
}

编辑:我不得不承认,这code实际上是pretty不好解决,因为这会是长名单很慢。每一个元素被添加到A1时,列表的一个大的部分必须由一个位置移动。
所以下面从MadProgrammer的建议,这里是一个更好的和多更快的方法来做到这一点。

 私有静态无效交错(ArrayList的<整数GT; A1,ArrayList的<整数GT; A2){
    ArrayList的<整数GT; R =新的ArrayList&所述;>(a1.size()+ a2.size());    的for(int i = 0,J = 0; I< a1.size()|| J< a2.size();我++,J ++){
        如果(ⅰ&下; a1.size())r.add(a1.get(ⅰ));
        如果(J< a2.size())r.add(a2.get(J));
    }
    a1.clear();
    a1.addAll(R);
}

I am having trouble getting this thing to run. I'm not sure if what I have so far is on the right track. I'm not quite sure where is is giving me an out of bounds error.

Here are the instructions:

Write a method called interleave that accepts two ArrayLists of integers a1 and a2 as parameters and inserts the elements of a2 into a1 at alternating indexes. If the lists are of unequal length, the remaining elements of the longer list are left at the end of a1. For example, if a1 stores [10, 20, 30] and a2 stores [4, 5, 6, 7, 8], the call of interleave(a1, a2); should change a1 to store [10, 4, 20, 5, 30, 6, 7, 8]. If a1 had stored [10, 20, 30, 40, 50] and a2 had stored [6, 7, 8], the call of interleave(a1, a2); would change a1 to store [10, 6, 20, 7, 30, 8, 40, 50].

private static void interleave(ArrayList<Integer> a1,
        ArrayList<Integer> a2) {

    int i = a1.size();
    int j = a2.size();

    if (i < j) { // a1 is shorter than a2
        for (int k = 0; k < a1.size(); k++) { // before k passes a1 size
            a1.add(k+1, a2.get(k));
        }

        for (int l = a1.size(); l < a2.size(); l++) {
            a1.add(a1.size(), a2.get(l));
        }

    } else if (i > j) { // a1 is longer than a2
        for (int k = 1; k < a2.size(); k++) {
            a1.add(k+1, a2.get(k));
        }

    } else { // they are equal length
        for (int k = 1; k < a2.size(); k++) {
            a1.add(k+1, a2.get(k));
        }
    }
}

解决方案

This should work

private static void interleave(ArrayList<Integer> a1, ArrayList<Integer> a2) {
    int i = -1;
    for(Integer elem: a2) {
        if(i < a1.size()-1) {
            i += 2;
        } else {
            i += 1;
        }
        a1.add(i, elem);
    }
}

public static void main(String[] args) throws Exception {

    ArrayList<Integer> a1 = new ArrayList<>(Arrays.asList(10, 20, 30));
    ArrayList<Integer> a2 = new ArrayList<>(Arrays.asList(4, 5, 6, 7, 8));

    interleave(a1, a2);
    System.out.println(a1);
}

edit: I have to admit, that this code is actually a pretty bad solution, because it's gonna be very slow for long lists. Every time an element is added to a1, a big part of the list has to be shifted by one position. So following the advice from "MadProgrammer", here is a much better and much much faster way to do it

private static void interleave(ArrayList<Integer> a1, ArrayList<Integer> a2) {
    ArrayList<Integer> r = new ArrayList<>(a1.size() + a2.size());

    for(int i = 0, j = 0; i < a1.size() || j < a2.size(); i++, j++) {
        if(i < a1.size()) r.add(a1.get(i));
        if(j < a2.size()) r.add(a2.get(j));
    }
    a1.clear();
    a1.addAll(r);
}

这篇关于2的ArrayList之间交替。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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