组合两个ArrayLists

时间:2013-11-14 06:10:26

标签: java arraylist

我必须将两个arraylists交错组合成一个arraylist,但我似乎无法解决这个问题。当列表1的大小更大时,或者列表2的大小更大时,代码应该工作。

我还没有学习以下代码中未包含的任何方法或术语。

非常感谢帮助。谢谢!以下是我到目前为止的情况:

public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{ 

    ArrayList<String> tempList = new ArrayList<>();

    int count = 1; 
    int length = list1.size() + list2.size();
    String temp1 = "";
    String temp2 = "";
    boolean test = true;

    if (list1.size() >= list2.size())
    {
        for (int j = 0; j<length; j++)
        {
            for (int i = 0; i < length ; i++)
            {
                tempList.add(2*i, list1.get(i));
                tempList.add(2*i+1, list2.get(i));
                count++;
                if (count == list2.size())
                {
                    break;
                }
            }
        }
        list1.clear();
        list1.addAll(tempList);
    }
    else
    {
        for (int i = 0; i<length; i++)
        {
            list1.add(2*i+1, list2.get(i));
            count++;
            if (count == list1.size())
            {
                break;
            }
        }
    }
public static void main(String[] args) 
{ 
    ArrayList list1 = new ArrayList(); 
    list1.add(2); 
    list1.add(3); 
    list1.add(7); 

    ArrayList list2 = new ArrayList();
    list2.add("Hi?"); 
    list2.add("name"); 
    list2.add("mallory");
    list2.add("nice"); 

    ArrayList list3 = new ArrayList(); 
    list3.add("my"); 
    list3.add("is"); 
    list3.add("pizzazz"); 

    interLeave(list2, list3);
    System.out.print(list2);

3 个答案:

答案 0 :(得分:1)

如何(只是为了简化)

public static void interLeave(ArrayList<String> list1, ArrayList<String> list2) 
{ 

    ArrayList<String> tempList = new ArrayList<String>();

    int length = Math.max(list1.size(), list2.size());

    for (int j = 0; j<length; j++)
    {
        if (j < list1.size()) {
            tempList.add(2*j, list1.get(j));
        }
        if (j < list2.size()) {
            tempList.add(2*j+1, list2.get(j));
        }
    }
    list1.clear();
    list1.addAll(tempList);
}

答案 1 :(得分:0)

试试这个

  import java.util.ArrayList;

public class Test{

public static void interLeave(ArrayList<String> list1,
        ArrayList<String> list2) {

    ArrayList<String> tempList = new ArrayList<>();

    int count = 1;
    int length = list1.size() + list2.size();
    String temp1 = "";
    String temp2 = "";
    boolean test = true;

    if (list1.size() >= list2.size()) {
        for (int j = 0; j < list2.size(); j++) {
            for (int i = 0; i < list2.size(); i++) {
                tempList.add(2 * i, list1.get(i));
                tempList.add(2 * i + 1, list2.get(i));
                count++;
                if (count == list2.size()) {
                    break;
                }
            }
        }
        list1.clear();
        list1.addAll(tempList);
    } else {
        for (int i = 0; i < length; i++) {
            list1.add(2 * i + 1, list2.get(i));
            count++;
            if (count == list1.size()) {
                break;
            }
        }
    }
}

public static void main(String[] args) {
    ArrayList list1 = new ArrayList();
    list1.add(2);
    list1.add(3);
    list1.add(7);

    ArrayList list2 = new ArrayList();
    list2.add("Hi?");
    list2.add("name");
    list2.add("irene");
    list2.add("nice");

    ArrayList list3 = new ArrayList();
    list3.add("my");
    list3.add("is");
    list3.add("Lee");

    interLeave(list2, list3);
    System.out.print(list2);
}
}

输出

 [Hi?, my, name, is, irene, Lee, Hi?, my, name, is, irene, Lee, Hi?, my, name, is]

答案 2 :(得分:0)

还有一个解决方案。

public static void interLeave(ArrayList<String> list1, ArrayList<String> list2) 

{

ArrayList<String> tempList = new ArrayList<>();

Iterator<String> Iterator1 = list1.iterator();  
Iterator<String> Iterator2 = list2.iterator();  

while (Iterator1.hasNext() && Iterator2.hasNext())  
{  
    tempList.add(Iterator1.next());  
    tempList.add(Iterator2.next()); 
}
while (Iterator1.hasNext()){
    tempList.add(Iterator1.next()); 
}
while (Iterator2.hasNext()){
    tempList.add(Iterator2.next()); 
}
list1.clear();
list1.addAll(tempList);

}