检查 ArrayList 的值 - IN A ROW - 是否相同
Check if values of an ArrayList - IN A ROW - is the same
我有一个问题似乎还没有找到答案。
所以我有一个 ArrayList 包含两种类型的值:Won 或 Loss。这些值的顺序不同。我需要检查相同的值有多少次来自数组的顶部,以便显示例如一个用户连续赢了 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]
谁能解决这个问题?我已经尝试了一段时间,但不幸的是我还没有找到解决方案。 T
//假设List最少有一个元素
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 );
我有一个问题似乎还没有找到答案。 所以我有一个 ArrayList 包含两种类型的值:Won 或 Loss。这些值的顺序不同。我需要检查相同的值有多少次来自数组的顶部,以便显示例如一个用户连续赢了 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]
谁能解决这个问题?我已经尝试了一段时间,但不幸的是我还没有找到解决方案。 T
//假设List最少有一个元素
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 );