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

时间:2012-03-01 15:42:16

标签: java

我是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有类似的问题,但我不想使用外部库,而是想自己学习如何创建这个功能。

2 个答案:

答案 0 :(得分:1)

广义算法看起来像这样(假设您想要输入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;
}

答案 1 :(得分:1)

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