【问题标题】:How to alter specific rows of 2d list based on value of boolean dependent on the list如何根据取决于列表的布尔值更改二维列表的特定行
【发布时间】:2012-02-01 22:34:58
【问题描述】:

如何在不遍历整个数组列表的情况下将此布尔 T/F 检查合并到此代码中>?

情况如下;

arraylist<arraylist<Integer>> bigArray....elig arraylist<Boolean>
elig.size() == bigArray.get(0).size();
bigArray is rectangular (i.e. no differing sizes of interior Lists

我的代码如下:

for(int j=0; j<(bigArr.size()); j++) {  //lets assume this is ==10           
  for(int e=0; e<(ballotArr.get(0).size()); e++) {   //assume == 5
    if(elig.get(e) == false) {          
        for(int k=0; k<(ballotArr.get(0).size()); k++) {  //==5
            bigArr.get(j).set(k, big.get(j).get(k)-1);
        }
    }
  } 
}

可以清楚地看到,这将在 elig.get(e)==false 任何时候通过最内部的 for 循环循环 10 次,并从所有索引中减去。

需要的是 elig.get(e) 在 for(int k) 处理时保持一个稳定的值,然后得到下一个值(至少我相信这是解决方案)

目标是从特定列中值为 1 的所有行中减去 1。

感谢您的任何帮助/建议。

【问题讨论】:

    标签: java arrays list multidimensional-array arraylist


    【解决方案1】:

    使用库实现,可以稍微加快速度。使用自定义集合可能会更快,但这超出了这里的范围。您正在遇到某些迭代限制,因此无论如何您只能做这么多。

    将过程分解成更小的方法会很有帮助:

    public static void decrementColumns(List<List<Integer>> rows, List<Boolean> mask) {
        final List<Integer> maskIndicies = getMaskIndicies(mask);
        // We're locked into this iteration because we have to modify every row.
        for (List<Integer> row : rows) {
            apply(maskIndicies, row);
        }
    }
    
    // Your big savings will come from figuring out the indicies.
    // This allows us to make the iterations-per-row smaller - 
    //   assuming not every row (or even most) is set to 'true'!
    public static List<Integer> getMaskIndicies(List<Boolean> mask) {
        final List<Integer> maskIndicies = new ArrayList<Integer>(mask.size());
        for (int i = 0; i < mask.size(); i++) {
            if (mask.get(i)) {
                maskIndicies.add(i);
            }
        } 
    }
    
    public static void apply(List<Integer> maskIndicies, List<Integer> row) {
        // We're locked into this iteration, needing to apply the transformation
        // to every column included.
        for (Integer index : maskIndicies) {
            final Integer modified = row.get(index) - 1;
            row.set(index, modified);
        }
    }
    

    请注意,这不是线程安全的,所以要小心。我也没有写任何安全检查,所以...


    编辑:

    重新阅读该问题后,我意识到我最初误读了代码在做什么(而且我在踢自己 - 不知何故我掉了一个循环)。

    修改后的版本:

    public static void decrementColumns(List<List<Integer>> rows, List<Boolean> mask) {
        final int count = getMaskCount(mask);
        // We're locked into this iteration because we have to modify every row.
        for (List<Integer> row : rows) {
            apply(row, count);
        }
    }
    
    public static void int getMaskCount(List<Boolean> mask) {
        int count = 0;
        for(Boolean flag : mask) {
            if (!flag) {
                count++;
            }
        }
        return count;
    }
    
    public static void apply(List<Integer> row, int count) {
        for (int index = 0; index < row.size(); index++) {
            final Integer modified = row.get(index) - count;
            row.set(index, modified);
        }
    }
    

    请注意,这个仍然并没有做〜完全〜你的原始代码所做的,只是我假设你正在尝试做的事情,给定你的“要求”文本。一方面,您至少定义了 2 个附加列表,但您没有给出关系 - 我相当肯定其中一个是错字。如果您为了清楚起见而编辑您的问题,我可能会提供更好的答案;您的代码和问题文本之间存在一些模棱两可或矛盾的事情。请注意,虽然您的原始代码在 O(m * (n ^ 2)) 中运行,但最低限度(和我的版本)在 O(n + (m * n)) 中运行。

    【讨论】:

    • 对我这样做的任何建议(即没有稍微加快速度)?我真的很想弄清楚这件事的逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2022-06-14
    • 2013-09-11
    • 2018-04-20
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多