【问题标题】:I want to compare every single element in arrays of a ArrayList<Player> with an int[ ] array我想将 ArrayList<Player> 数组中的每个元素与 int[ ] 数组进行比较
【发布时间】:2016-04-05 18:49:14
【问题描述】:

所以我有一个带有对象 Player 的 ArrayList,其中包含带有元素的数组 (ArrayList) 和另一个数组。 问题是:**我想将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);
   }

感谢您的帮助! 提前致谢

【问题讨论】:

  • 问题是什么?
  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。
  • 我想将 Player 类中的元素与我的其他数组 b 进行比较

标签: java object arraylist compare


【解决方案1】:

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

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

【讨论】:

  • 我想比较数组中的元素。所以首先从播放器中获取它们然后比较它们,所有数组将只包含 7 个元素
【解决方案2】:

在 Player 类中添加这一行

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));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多