-1

我必须将两个数组列表交错到一个数组列表中,但我似乎无法解决这个问题。当列表 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);
4

3 回答 3

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);
}
于 2013-11-14T06:26:54.920 回答
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]
于 2013-11-14T06:19:48.957 回答
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);

}

于 2013-11-14T06:27:56.327 回答