【问题标题】:Java complex Iteration through a list arrayJava通过列表数组进行复杂迭代
【发布时间】:2018-07-18 01:53:26
【问题描述】:

我想遍历列表数组中的重复数字组,并在组更改时执行某些操作。我的问题是当下一个组包含相同的数字时,我如何分隔两组?例如,我在数组中有以下数字: 3,3,3,10,10,10,10,10,10,10,10,10,10,5,5,5,5,5 总是有 3 个 3 和 10 个 10 等等。但如果我有 3,3,3,3,3,3 这是两组三个我变得不卡住了。

一种(某种)可行的解决方案是:

List<Integer> changeOfTableCell = new ArrayList<Integer>();
changeOfTableCell.add(3);
changeOfTableCell.add(3);
changeOfTableCell.add(3);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(10);
changeOfTableCell.add(5);
changeOfTableCell.add(5);
changeOfTableCell.add(5);
changeOfTableCell.add(5);
changeOfTableCell.add(5);

int startCounter=changeOfTableCell.get(0);
int counter=0;

for(int a=0; a<changeOfTableCell.size(); a++) { 

    if(startCounter==changeOfTableCell.get(counter)) { //the start of the group
        for(int i=0; i<startCounter; i++) { //print the group as per the size of the group

            System.out.println(changeOfTableCell.get(counter));
            counter=counter+1;
        }

        //move the start of the next group
        startCounter=changeOfTableCell.get(counter)
        System.out.println("Break");//do something here
    }
}

计数器变为 18,这给了我一个索引越界异常。解决方案对我来说并不明显,它一直让我发疯!

提前谢谢你。 菲尔

【问题讨论】:

  • 尝试查看值而不是迭代例如如果 [3,3,3,3,3,3,2,2] 查看第一个值 3 (i=0) 然后从 i=0,1,2 开始分组 下一个值为 3 (i=3) 组 i= 3,4,5 下一个值是 2(i=6) 所以组 i=6,7
  • 这个主意不错,我试试看。

标签: java arraylist iteration


【解决方案1】:

在你的第二个 for 循环之后,你应该有一个 if 守卫来保护最后一次迭代。这是 counter 等于 List 的大小的时间,因为内部 for 循环的最终迭代将计数器增加 1

if(counter < changeOfTableCell.size()){
  startCounter=changeOfTableCell.get(counter);
}

或者更确切地说是终止

if(counter >= changeOfTableCell.size()) break;
startCounter=changeOfTableCell.get(counter);

【讨论】:

    【解决方案2】:

    如果您想在组更改时执行某些操作,则无需迭代列表。下一个值的索引是当前索引+当前值。

    interface OnGroupChangeListener {
        void onChange(int oldValue, int newValue);
    }
    
    static void test(List<Integer> list, OnGroupChangeListener listener) {
        int idx = 0;
        while (true) {
            int value = list.get(idx);
            idx += value;
            if (idx >= list.size()) break;
            listener.onChange(value, list.get(idx));
        }
    }
    
    public static void main(String[] args) {
    
        List<Integer> list = Arrays.asList(1, 1, 2, 2, 3, 3, 3, 3, 3, 3);
    
        test(list, new OnGroupChangeListener() {
            @Override
            public void onChange(int oldValue, int newValue) {
                System.out.println(oldValue + " -> " + newValue);
            }
        });
    }
    

    【讨论】:

    • 感谢 zhh(以及所有)您的回答帮助我摆脱了“铁路轨道”的想法,并从不同的角度看待它。
    【解决方案3】:

    计数器变为 18 并引发 IndexOutOfBoundsException,因为 counter 正在跟踪它已迭代的元素数量。打印出列表中的最后一个值后,startCounter 尝试获取第 18 个索引处的元素,该索引不存在(列表为 0 索引)。要修复它,请执行条件语句来检查 counter 是否已经在第 18 个索引处。

    【讨论】:

      【解决方案4】:

      这是我想出的解决方案:

      public class CounterTest {
      
      public static void main(String[] args) {
      
          List<Integer> changeOfTableCell = new ArrayList<Integer>();
          changeOfTableCell.add(3);
          changeOfTableCell.add(3);
          changeOfTableCell.add(3);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(10);
          changeOfTableCell.add(5);
          changeOfTableCell.add(5);
          changeOfTableCell.add(5);
          changeOfTableCell.add(5);
          changeOfTableCell.add(5);
          int start=0;
          int length=changeOfTableCell.get(0);
      
      
          for(int a=0; a<changeOfTableCell.size(); a++){
      
          for(int i=start; i<length; i++) {
      
              System.out.println(changeOfTableCell.get(start));
      
          }if(length==changeOfTableCell.size()){
              System.out.println("Do something ");break;
              }else start=length; length=length+changeOfTableCell.get(start);
                  System.out.println("Do something ");
      }
      

      }

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 2015-10-04
        • 2013-12-27
        • 2014-12-02
        • 2018-07-19
        相关资源
        最近更新 更多