我想将 ArrayList 数组中的每个元素与<Player> int[ ] 数组进行比较



所以我有一个ArrayList,它有一个对象Player,它有包含元素的数组(ArrayList)和另一个数组。问题是:**我想将数组列表中对象Player中数组中的**元素与其他数组进行比较。

这是我班的播放器

public class Player{
  private String name;
  private int bet;
  private ArrayList <int []> lottorows;

这是我目前比较的代码:

    //Method that calculates no of right rows and puts it to the player
public static void NoOfRights(ArrayList<Player> a, int[] b){
    int count = 0;
    for(int i = 0; i < a.size(); i++){
        Player player = a.get(i);
        for(int j = 0; j < player.getRows();j++) {
               boolean isEqual = isEquals(player.getRows(j), b, 7);
               if(isEqual == true){
                   count++;
               }
               if(count == 7){
                   player.noOf7right(count);
               }
               else if(count == 6){
                   player.noOf6right(count);
               }
               else if(count == 5){
                   player.noOf5right(count);
               }

isEqual是一种方法:

    private static boolean isEquals(int[] j, int[] b, int size) {
    for(int i = 0; i<size; i++) {
        if(j[i] == b[i]) {
             return true;
        }
    }
    return false;
}

.getRows()是Player:中的一个方法

public int[] getRows(int a){
      return lottorows.get(a);
   }

我会感谢你的帮助!提前感谢

这归结为比较数组。您可以使用:

if(Arrays.equals(array1, array2)) count++;

在播放器类中添加此行

private int SumOfRIghts;

这两种方法

public void setSumOfRights(int sum){
    SumOfRights = sum;
}
public int NumOfRights(int [] numbers){
    int count = 0;
    for(int i = 0; i<lottorows.size();i++){
        for (j = 0; j<numbers.lenght){
            if(lottorows.get(i)==numbers[j]) count++;
        }
    }
}
return count;

然后你可以做

public static void sumOfRights(ArrayList<Player> a, int[] b){
    for (int i = 0; i < a.size(); i++) {
        a.get(i).setSumOfRights(a.get(i).NumOfRights(b));
    }
}