如何创建合并其他两个列表的列表?

问题描述:

我真的是新来的java和慢慢学习,所以不知道是否有一个明显的方法来做到这一点,但我基本上有两个列表,我想合并在一起,形成一个单子。如何创建合并其他两个列表的列表?

为此的Python代码使用称为zip的函数。假设我有list1 = 1,2,3,4,5list2= 6,7,8,9,10 ..然后我想用new_list = (1,6), (2,7), (3,8), (4,9), (5,10)这样的东西创建一个新列表。

我发现有一个question有类似的问题,但我不想使用外部库,而宁愿学习如何自己创建此功能。

+1

最好的学习方法是尝试自己做。我建议你写一个帮助器方法(你可以称它为“zip”;) – 2012-03-01 15:46:01

+1

同时迭代两个列表,并从每个相同的偏移量创建一个对,然后将这个对放在一个新列表中。 – 2012-03-01 15:46:17

+0

“学习如何自己创建此功能”,通过询问如何在*上执行此操作? – Gray 2012-03-01 16:35:12

广义的算法会是这个样子(假设你要取N输入列表):

public <T> List<List<T>> zip(List<T> ... lists) { 

    if(lists.isEmpty()) { 
     return Collections.<List<T>>emptyList(); 
    } 

    // validate that the input lists are all the same size. 
    int numItems = lists[0].size(); 
    for(int i = 1; i < lists.length; i++) { 
     if(lists[i].size() != numItems) { 
      throw new IllegalArgumentException("non-uniform-length list at index " + i); 
     } 
    } 

    List<List<T>> result = new ArrayList<List<T>>(); 

    for(int i = 0; i < numItems; i++) { 

     // create a tuple of the i-th entries of each list 
     List<T> tuple = new ArrayList<T>(lists.length); 
     for(List<T> list : lists) { 
      tuple.add(list.get(i)); 
     } 

     // add the tuple to the result 
     result.add(tuple); 
    } 

    return result; 
} 
+0

如果'lists'中的列表中有一个元素与另一个列表中的元素较少,则您的代码可以抛出'IndexOutOfBoundsException'。 – 2012-03-01 15:52:33

+0

我大多是懒惰的,而不是写错误检查逻辑。 :)我喜欢积极地进行验证,所以我会在开始时进行快速检查(特别是因为我使用第一个列表的大小作为循环条件)。 – stevevls 2012-03-01 15:54:43

public class Blammy 
{ 
    private String left; 
    private String right; 

    public Blammy(final String left, final String right) 
    { 
     this.left = left; 
     this.right = right; 
    } 

    public String toString() 
    { 
     return "(" + left + ", " + right + ")"; 
    } 
} 

public class Kramlish 
{ 
    public List<Blammy> mergalish(final List<String> left, final List<String> right) 
    { 
     int leftSize; 
     int maxSize; 
     int rightSize; 
     String leftValue; 
     List<Blammy> returnValue; 
     String rightValue; 

     if (left != null) 
     { 
      leftSize = left.size(); 
     } 
     else 
     { 
      leftSize = 0; 
     } 

     if (right != null) 
     { 
      rightSize = right.size(); 
     } 
     else 
     { 
      rightSize = 0; 
     } 

     if (leftSize > rightSize) 
     { 
      maxSize = leftSize; 
     } 
     else 
     { 
      maxSize = rightSize; 
     } 

     if (maxSize > 0) 
     { 
      returnValue = new ArrayList<Blammy>(maxSize); 

      for (int index = 0; index < maxSize; ++index) 
      { 
       if (index < leftSize) 
       { 
        leftValue = left.get(index); 
       } 
       else 
       { 
        leftValue = null; 
       } 

       if (index < rightSize) 
       { 
        rightValue = right.get(index); 
       } 
       else 
       { 
        rightValue = null; 
       } 

       Blammy item = new Blammy(leftValue, rightValue); 
       returnValue.add(item); 
      } 
     } 
     else 
     { 
      returnValue = new ArrayList<Blammy>(); 
     } 

     return returnValue; 
    } 
}