Java 从一个列表填充多个列表

Java 从一个列表填充多个列表,java,arraylist,Java,Arraylist,如何从一个列表中填充三个ArrayList,将值从index1填充到lista1,将值从index2填充到lista2,将值从index3填充到lista3,将值从index4填充到lista1等等 ArrayList<String> lista = new ArrayList<String>(); ArrayList<String> lista1 = new ArrayList<String>(); ArrayL

如何从一个列表中填充三个ArrayList,将值从index1填充到lista1,将值从index2填充到lista2,将值从index3填充到lista3,将值从index4填充到lista1等等

ArrayList<String> lista = new ArrayList<String>();

        ArrayList<String> lista1 = new ArrayList<String>();
        ArrayList<String> lista2 = new ArrayList<String>();
        ArrayList<String> lista3 = new ArrayList<String>();

        sh.forEach(row -> {
                row.forEach(cell -> {
                String cellvalue = dataFormatter.formatCellValue(cell);
                lista.add(cellvalue);
            });
        });

您可以将三个列表添加到三元素数组中,并使用传统的for循环对它们进行迭代。这是修改后的代码

    ArrayList<String> lista = new ArrayList<String>();
    ArrayList<String>[] lists = new ArrayList[3];

    lists[0] = new ArrayList<String>();
    lists[1] = new ArrayList<String>();
    lists[2] = new ArrayList<String>();


    for(int i = 0; i < lista.size(); i++){
        lists[i % lists.length].add(lista.get(i));
    }

您可以将三个列表添加到三元素数组中,并使用传统的for循环对它们进行迭代。这是修改后的代码

    ArrayList<String> lista = new ArrayList<String>();
    ArrayList<String>[] lists = new ArrayList[3];

    lists[0] = new ArrayList<String>();
    lists[1] = new ArrayList<String>();
    lists[2] = new ArrayList<String>();


    for(int i = 0; i < lista.size(); i++){
        lists[i % lists.length].add(lista.get(i));
    }

Java8+流API,你可以试试这个

    List<String> lista = Arrays.asList("A","B","C","D","E");

    List<String> list1 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 0)
            .collect(Collectors.toList());

    List<String> list2 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 1)
            .collect(Collectors.toList());

    List<String> list3 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 2)
            .collect(Collectors.toList());

Java8+流API,你可以试试这个

    List<String> lista = Arrays.asList("A","B","C","D","E");

    List<String> list1 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 0)
            .collect(Collectors.toList());

    List<String> list2 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 1)
            .collect(Collectors.toList());

    List<String> list3 = lista.stream()
            .filter(x -> lista.indexOf(x) % 3 == 2)
            .collect(Collectors.toList());

什么是index1、index2和index3?lista的第一个、第二个和第三个索引?应该是ArrayList lista的索引。元素1、元素2、元素3等是否有任何属性适用于列表1中的那些,并且仅适用于列表1中的那些,或者其他属性?1 1 2 1 3 1 4 1 5 1 6 1--这是列表A的值。我希望第一个值在列表A1中,第二个在列表A2中,第三个在列表A3中。然后是新行,第四行到列表A1,第五行到列表A2,第六行到列表A3…您必须迭代所有行,检查索引%3并在此基础上添加。什么是index1、index2和index3?lista的第一个、第二个和第三个索引?应该是ArrayList lista的索引。元素1、元素2、元素3等是否有任何属性适用于列表1中的那些,并且仅适用于列表1中的那些,或者其他属性?1 1 2 1 3 1 4 1 5 1 6 1--这是列表A的值。我希望第一个值在列表A1中,第二个在列表A2中,第三个在列表A3中。然后是新行,列表A1的第四行,列表A2的第五行,列表A3的第六行……您必须迭代所有这些行,检查索引%3并在此基础上添加。