猿问

通过列表数组的Java复杂迭代

我想遍历列表数组中的重复数字组,并在组更改时执行某些操作。我的问题是当下一组包含相同的数字时,如何将两组分开?例如,我在一个数组中3,3,3,10,10,10,10,10,10,10,10,10,10,5,5,5,5,5 有以下数字: 总是有 3 个三分球和十个十分球等。但是如果我有 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,这给了我一个索引越界异常。解决方案对我来说并不明显,它一直让我发疯!


慕妹3242003
浏览 156回答 3
3回答

饮歌长啸

在您的第二个 for 循环之后,您应该有一个 if 保护来保护最后一次迭代。这哪里是时间counter等于的大小List,因为内部的for循环的最后一次迭代的加1计数器if(counter < changeOfTableCell.size()){&nbsp; startCounter=changeOfTableCell.get(counter);}或者更确切地说是终止if(counter >= changeOfTableCell.size()) break;startCounter=changeOfTableCell.get(counter);

繁星点点滴滴

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

慕丝7291255

这是我想出的解决方案:public class CounterTest {public static void main(String[] args) {&nbsp; &nbsp; List<Integer> changeOfTableCell = new ArrayList<Integer>();&nbsp; &nbsp; changeOfTableCell.add(3);&nbsp; &nbsp; changeOfTableCell.add(3);&nbsp; &nbsp; changeOfTableCell.add(3);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(10);&nbsp; &nbsp; changeOfTableCell.add(5);&nbsp; &nbsp; changeOfTableCell.add(5);&nbsp; &nbsp; changeOfTableCell.add(5);&nbsp; &nbsp; changeOfTableCell.add(5);&nbsp; &nbsp; changeOfTableCell.add(5);&nbsp; &nbsp; int start=0;&nbsp; &nbsp; int length=changeOfTableCell.get(0);&nbsp; &nbsp; for(int a=0; a<changeOfTableCell.size(); a++){&nbsp; &nbsp; for(int i=start; i<length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(changeOfTableCell.get(start));&nbsp; &nbsp; }if(length==changeOfTableCell.size()){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do something ");break;&nbsp; &nbsp; &nbsp; &nbsp; }else start=length; length=length+changeOfTableCell.get(start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do something ");}}}
随时随地看视频慕课网APP

相关分类

Java
我要回答