我有一个问题似乎找不到答案。
因此,我有一个由2种类型的值组成的ArrayList:获胜或亏损。这些值以不同的顺序出现。我需要检查多少次相同的值来自数组的顶部以显示例如使用者连续赢得5次。

ArrayList <String> wins = new ArrayList<String>();
System.out.print(wins);

[Won, Won, Won, Loss, Loss, Won, Won, Loss, Loss, Loss, Loss, Loss, Won, Won, Won, Won, Won, Loss]


谁能解决这个问题?我已经尝试了一段时间,但不幸的是我还没有找到解决方案。 Ť

最佳答案

//假设列表中至少包含一个元素

List<String> wins = Arrays.asList(new String[]{"Won", "Won", "Won", "Loss", "Loss", "Won", "Won", "Loss", "Loss", "Loss", "Loss", "Loss", "Won", "Won", "Won", "Won", "Won", "Loss"});
        System.out.println(wins);
        String value = wins.get(0);
        int maxValue=1;
        int count = 1 ;
        for(int i=1;i<wins.size();i++){
            if(wins.get(i) == wins.get(i-1)){
                count++;
                if(count >= maxValue){
                    maxValue = count;
                    value = wins.get(i);
                }
            }else{
                count=1;
            }
        }
        System.out.println(value + " : " + maxValue );

10-04 15:19