所以我有一个带有对象Player的ArrayList,该对象Player具有其中包含元素的数组(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);
}
isEquing是一种方法:
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;
}
. getRow()是Player中的一个方法:
public int[] getRows(int a){
return lottorows.get(a);
}
我将感谢你的帮助!提前感谢
这归结为比较数组。您可以使用:
if(Arrays. equals(array1,array2))计数;
在Player类中添加此行
private int SumOfRIghts;
和这2种方法
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));
}
}