我想遍历列表数组中的重复数字组,并在组更改时执行某些操作。我的问题是当下一组包含相同的数字时,如何将两组分开?例如,我在一个数组中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,这给了我一个索引越界异常。解决方案对我来说并不明显,它一直让我发疯!
饮歌长啸
繁星点点滴滴
慕丝7291255
相关分类