在两个数组列表之间交替. [英] Alternating between two arraylists.

查看:27
本文介绍了在两个数组列表之间交替.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法运行这个东西.我不确定到目前为止我所拥有的是否在正确的轨道上.我不太确定是哪里给了我越界错误.

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.

以下是说明:

编写一个名为 interleave 的方法,它接受两个整数 a1 和 a2 的 ArrayList 作为参数,并将 a2 的元素插入到 a1 的交替索引处.如果列表长度不等,则较长列表的其余元素留在 a1 的末尾.例如a1存储[10,20,30],a2存储[4,5,6,7,8],interleave(a1,a2);应该将 a1 更改为存储 [10, 4, 20, 5, 30, 6, 7, 8].如果 a1 已经存储了 [10, 20, 30, 40, 50] 并且 a2 已经存储了 [6, 7, 8],则调用 interleave(a1, a2);将更改 a1 以存储 [10, 6, 20, 7, 30, 8, 40, 50].

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

推荐答案

这应该可行

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

我不得不承认,这段代码实际上是一个非常糟糕的解决方案,因为对于长列表来说它会非常慢.每次向 a1 添加元素时,列表的很大一部分都必须移动一个位置.所以按照MadProgrammer"的建议,这里有一个更好、更快的方法

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

这篇关于在两个数组列表之间交替.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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