作者:手机用户2502884755 | 来源:互联网 | 2022-12-02 16:09
我想定义一个名为countRepeats的方法,它接受一个数字列表0到9,并返回相邻重复字母的出现次数.
例如,
测试用例1:数组{0,1,2,2,1,2,2,1,3,3,1}有三次重复数字
测试用例2:数组{0,1,1,1,1,2}有一次出现
以下是我的代码:
List intlist = new ArrayList();
int [] array = new int[]{};
while(sc.hasNext()){
intlist.add(sc.nextInt());
array = intlist.stream().mapToInt(i->i).toArray();
}
System.out.println("Number of occurrences: " + countRepeats(array));
public static long countRepeats(int [] array){
return IntStream.range(0, array.length-1)
.filter(n -> array[n] > 0)
.filter(i -> (array[i] == array[i+1]))
.peek(System.out::println)
.count();
}
但是,我的测试用例2没能得到预期的结果.任何人都可以启发我吗?