【问题标题】:Create subarraylist with condition创建带有条件的子数组列表
【发布时间】:2022-01-11 22:12:58
【问题描述】:

我在获取SubarrayLists 时遇到问题。 我有一个班级Accgy。它包括一个 Excelsheet 的 4 个单元格(单元格 y、x、z 、acc)。

    public class Accgy {

        Cell y;
        Cell x;
        Cell z;
        Cell acc;

        public Accgy(Cell x, Cell y, Cell z, Cell acc) {
            this.y = y;
            this.x = x;
            this.z = z;
            this.acc = acc;
        }

        Cell getCellz() { return z; }
        Cell getCelly() { return y; }
        Cell getCellx() { return x; }
        Cell getCellacc() { return acc; }

    }

在 Excelsheet 中,A x,y 和 z 是一行。在ArrayListaccgy里面有Excelsheet的所有值

我有来自 Arraylist maxPerRange 中另一个 Excelsheet B 的值。 在这个 Arraylist 中,只有 Excelsheet B 的最高值。 两个 Excelsheet 的行数相同。

我想创建一个 Arraylist,它执行 ArrayList accgy 的子数组列表,索引从 1590 开始,到 accgy.size()-700 结束。但条件是如果ArrayListaccgyrowIndex的值与ArrayListmaxPerRangerowIndex的值相同,则要把ArrayList划分到subArrayLists中。

我想在 subArraylist 中的 ArrayList accgy 的所有值在 maxPerRangerowIndex 之间。所以maxPerRangerowIndex 应该像边框。 subArraylist 中的值应该从一个maxPerRange 到下一个maxPerRange

我希望能清楚地解释我的问题。

这是我到目前为止所拥有的代码。

我认为一个错误是partitions1.add(accgy.subList(i, accgy.size() - 700));

但我不确定。我希望有人可以帮助我。

        List<List<Accgy>> partitions1 = new ArrayList<>();

        for (int i = 1590; i < accgy.size() - 700; i++) {
            for (int j = 0; j < maxPerRange.size(); j++) {
                if (accgy.get(i).getCellx().getRowIndex() == maxPerRange.get(j).getRowIndex()) {
                    partitions1.add(accgy.subList(i, accgy.size() - 700));
                }
            }
        }

        for (List<Accgy> c : partitions1) {
            for (Accgy g : c) {
                System.out.println(g.getCellx().getNumericCellValue());
                System.out.println(g.getCellx().getRowIndex());

            }
        }

【问题讨论】:

    标签: java list arraylist collections sublist


    【解决方案1】:

    我已经解决了我的问题。使用此代码,我有相同数量的子数组列表,例如数组列表maxPerRange 的大小,数组列表maxPerRange 的值是它应该执行子数组列表的边界。

        List<List<Accgy>> partitions1 = new ArrayList<>();
            for (int i = 1590; i < accgy.size()-700; i++) {
                    for (int j=0;j< maxPerRange.size();j++) {
                        if (accgy.get(i).getCellx().getRowIndex() == maxPerRange.get(j).getRowIndex()) {
                            partitions1.add(accgy.subList(i,  maxPerRange.get(j+1).getRowIndex()));
                        }else if(accgy.get(i).getCellx().getRowIndex() >= maxPerRange.get(maxPerRange.size()-1).getRowIndex()){
                                break;
                            }
    
                    }
            }
    
    

    希望对遇到同样问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多