【问题标题】:How to create a list that merges two other lists?如何创建一个合并其他两个列表的列表?
【发布时间】:2012-03-01 15:42:15
【问题描述】:

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

【问题讨论】:

  • 最好的学习方法是自己动手。我建议你写一个辅助方法(你可以称之为zip;)
  • 同时迭代两个列表,并从每个相同的偏移量创建一对,然后将这对放在一个新列表中。
  • “学习如何自己创建这个函数”通过在 Stackoverflow 上询问如何做?

标签: java


【解决方案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;
}

【讨论】:

  • 如果lists 中的一个列表的元素少于另一个列表,您的代码可能会抛出IndexOutOfBoundsException
  • 我大部分时间都很懒惰,没有编写错误检查逻辑。 :) 我更喜欢积极地验证,所以我会在开始时快速检查(尤其是因为我使用第一个列表的大小作为循环条件)。
【解决方案2】:
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;
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多