通过迭代列表提高代码性能

Improve Code performance with iterating over List

我正在玩 Java 中的 List 数据结构。如果一个列表包含 n 个列表,returns 一个 n 个元素的列表,该列表通过从每个 n 个列表中选取元素而形成。

一个例子:

Input:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
Output:[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

Input:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
Output:[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

Input:[[1, 2], [3, 4], [5, 6]]
Output:[[1, 3, 5], [2, 4, 6]]

下面的代码可以工作,但它看起来像是一个修复程序,有什么方法可以只使用一个 for 循环来实现它。

public class ListTuple  
{
    @SuppressWarnings({ "unchecked" })
    public static void main(String args[])
    {
       List<List<Integer>> newFinalList = new ArrayList<List<Integer>>();
       List<Integer> newList1 = new ArrayList<Integer>();
       List<Integer> newList2 = new ArrayList<Integer>();
       List<Integer> list1 = Arrays.asList(1 , 2);
       List<Integer> list2 = Arrays.asList(3 , 4);
       List<Integer> list3 = Arrays.asList(5 , 6);
       List<Integer> list4 = Arrays.asList(7 , 8);
       List<Integer> list5 = Arrays.asList(9 , 10);
       List<List<Integer>> finalList = Arrays.asList(list1, list2, list3, list4, list5);
       for (List<Integer> elementOfFinalList : finalList)
       {
          newList1.add(elementOfFinalList.get(0));
       }
       for (List<Integer> elementOfFinalList : finalList)
       {
          newList2.add(elementOfFinalList.get(1));
       }
       newFinalList.add(newList1);
       newFinalList.add(newList2);
       System.out.println("Input:" + finalList);
       System.out.println("Output:" + newFinalList);
  }
}

同时,上面的代码将不起作用。

Input:[[1, 2, 3], [4, 5, 6]]
Output:[[1, 4], [2, 5], [3, 6]]

任何建议都会有所帮助。

谢谢!

您可以将两个 for 循环压缩为一个

for (List<Integer> elementOfFinalList : finalList)
{
    newList1.add(elementOfFinalList.get(0));
    newList2.add(elementOfFinalList.get(1));
}

这看起来和将要实现的一样紧凑


代码无效是什么意思?你的代码对我有用(带导入)

你说的这组inputs/outputs是什么意思?

Input:[[1, 2, 3], [4, 5, 6]]
Output:[[1, 4], [2, 5], [3, 6]]

这是一些数组 Class 给你的,我从“http://underscorejs.org/”代码中引用了它。我相信低于分数源代码的方式是最好的事情之一。


package com.list;

import java.util.ArrayList;

public class ListManagement {

    //picking value of idx and put on the resultList
    //list must be contained two depth list
    public static ArrayList map(ArrayList lists,int idx){

        ArrayList resultList = new ArrayList();

        for(ArrayList list : lists){
            resultList.add(list.get(idx));
        }

        return resultList;
    }

    //  Size Return
    public static int getLength(ArrayList lists){
        int resultLength = 0;
        for(ArrayList list : lists){
            resultLength = list.size() > resultLength ? list.size() : resultLength;
        }
        return resultLength;
    }

    public static ArrayList unzip(ArrayList lists){
        int length = getLength(lists);
        ArrayList resultList = new ArrayList();

        for(int j = 0; j  lists){
        ArrayList resultList = unzip(lists);

        return resultList;
    }

    public static void main(String[] args) {
        ArrayList list1 = new ArrayList();
        list1.add("aaa");
        list1.add("111");
        list1.add("!!!");
        list1.add("AAA");

        ArrayList list2 = new ArrayList();
        list2.add("bbb");
        list2.add("222");
        list2.add("@@@");
        list2.add("BBB");

        ArrayList list3 = new ArrayList();
        list3.add("ccc");
        list3.add("333");
        list3.add("###");
        list3.add("CCC");

        ArrayList mainList = new ArrayList();
        mainList.add(list1);
        mainList.add(list2);
        mainList.add(list3);

        ArrayList resultList = ListManagement.unzip(mainList);
        System.out.println("Un-Zipped Array : "+resultList);

        resultList = ListManagement.zip(resultList);
        System.out.println("Zipped Array : "+resultList);

    }

}

这是结果

Un-Zipped Array : [[aaa, bbb, ccc], [111, 222, 333], [!!!, @@@, ###], [AAA, BBB, CCC]]
Zipped Array : [[aaa, 111, !!!, AAA], [bbb, 222, @@@, BBB], [ccc, 333, ###, CCC]]