有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编写了一个合并两个数组列表的方法,交替使用两个数组列表中的元素

写一个方法

public static ArrayList merge(ArrayList a, ArrayList b)

合并两个数组列表,交替使用两个数组列表中的元素。如果一个数组列表比另一个数组列表短,则尽可能长地替换,然后从较长的数组列表中附加其余元素。例如,如果

1 4 9 16

b是

9 7 4 9 11

然后merge返回数组列表

1 9 4 7 9 4 16 9 11


我尝试使用if语句编写for循环,这样当I为偶数(I%2==0)时,从数组列表a向合并数组列表添加一个数字,当I为奇数时,从数组列表b向合并数组列表添加一个数字。但是,我不知道如何处理一个数组列表可能比另一个数组列表长的事实。谁能帮我一下吗

编辑:好的,下面是代码(但远远不正确):

public static ArrayList<Integer> merge(ArrayList<Integer> een, ArrayList<Integer> twee)
{
    ArrayList<Integer> merged = new ArrayList<Integer>();

    for(int i = 0; i<100; i++)
    {           
        if(i%2!=0)
        {
            merged.add(a.get(i));
        }   
        if(i%2 == 0)
        {
            merged.add(b.get(i));
        }               
    }

    System.out.println(merged);
    return merged;
}

共 (6) 个答案

  1. # 1 楼答案

    迭代器似乎最容易做到这一点

    public static <T> ArrayList<T> merge(Collection<T> a, Collection<T> b) {
        Iterator<T> itA = a.iterator();
        Iterator<T> itB = b.iterator();
        ArrayList<T> result = new ArrayList<T>();
    
        while (itA.hasNext() || itB.hasNext()) {
            if (itA.hasNext()) result.add(itA.next());
            if (itB.hasNext()) result.add(itB.next());
        }
    
        return result;
    }
    

    没有迭代器:

    public static <T> ArrayList<T> merge(List<T> a, List<T> b) {
        ArrayList<T> result = new ArrayList<T>();
        int size = Math.max(a.size(), b.size());
    
        for (int i = 0; i < size; i++) {
            if (i < a.size()) result.add(a.get(i));
            if (i < b.size()) result.add(b.get(i));
        }
    
        return result;
    }
    

    注意,我稍微放宽了方法签名。如果使用迭代器实现合并,Collection(甚至Iterable)就可以了。否则,List就可以了。没有必要要求ArrayList作为方法参数类型

  2. # 2 楼答案

    这是我的解决方案

    LinkedList<Integer> list3 = new LinkedList<Integer>();
    
    
    Iterator<Integer> itA = list.iterator();
    Iterator<Integer> itB = list2.iterator();
    
    while(itA.hasNext() && itB.hasNext()){
        list3.add(itA.next());
        list3.add(itB.next());
    }
    
  3. # 3 楼答案

    没有迭代器:

    public static ArrayList merge(ArrayList a, ArrayList b) {
        int c1 = 0, c2 = 0;
        ArrayList<Integer> res = new ArrayList<Integer>();
    
        while(c1 < a.size() || c2 < b.size()) {
            if(c1 < a.size())
                res.add((Integer) a.get(c1++));
            if(c2 < b.size())
                res.add((Integer) b.get(c2++));
        }
        return res;
    }
    
  4. # 4 楼答案

    我遇到了同样的情况,下面是我的解决方案:

    // array list to hold the merged list
    ArrayList<Integer> mergedList = new ArrayList<Integer>();
    
    // Get the bigger size
    int maxSize = listOne.size() > listTwo.size() ? listOne.size() : listTwo.size();
    
    // Loop thru the list
    for( int i = 0; i <= maxSize; i++){
        // We need to check first if index exist then just add it to mergeList
        if( i < listOne.size() ) mergedList.add( listOne.get( i ) );
        // We need to check first if index exist then just add it to mergeList
        if( i < listTwo.size() ) mergedList.add( listTwo.get( i ) );
    }
    
  5. # 5 楼答案

    您不需要检查模,否则将从每个输入列表中跳过每一秒的元素

    public static <E> List<E> merge(List<E> een, List<E> twee) {
        List<E> merged = new ArrayList<E>(een.size() + twee.size());
        List<E> shorter = een.size() <= twee.size() ? een : twee;
        List<E> longer = een.size() > twee.size() ? een : twee;
        for (int i = 0; i < shorter.size(); i++) {
            merged.add(een.get(i));
            merged.add(twee.get(i));
        }
        for (int i = shorter.size(); i < longer.size(); i++) {
            merged.add(longer.get(i));
        }
        return merged;
    }
    

    此泛型版本适用于所有类型的列表和泛型类型

  6. # 6 楼答案

    试试这个:我使用数组实现

    public static void main(String[] args) {
        int[] first = { 1, 4, 9, 16 };
        int[] second = { 9, 7, 4, 9, 11 };
        int[] merge = new int[first.length + second.length];
        int j = 0, k = 0, l = 0;
        int max = Math.max(first.length, second.length);
        for (int i = 0; i < max; i++) {
            if (j < first.length)
                merge[l++] = first[j++];
            if (k < second.length)
                merge[l++] = second[k++];
        }
        System.out.println(Arrays.toString(merge));
    }
    

    输出:

    [1, 9, 4, 7, 9, 4, 16, 9, 11]