一次遍历 Hashset 和 arraylist

looping through a Hashsets and array list at once

我有一个 for 循环,它创建了几个哈希集来存储个人数据

for (int i = 0; i < numberOfPlayers; i++) {
        players.add(new HashSet<>());
    }

that's for the players

for (int i = 0; i < numberOfPlayers; i++) {
        amount.add(new ArrayList<>());
    } //this is the arraylist that stores the score

再用一个for循环创建等量的ArrayList来存储分数

我知道如何评估我创建的单个 HashSet 和 ArrayList

 for (HashSet<Integer> player : players) {
        player = userLottery(player);
    } // to access the individual hashSets

for (ArrayList<Integer> total : totalScore) {
        storage.add(total); 
    } // to access the individual hashSets

但我想一次遍历各个项目。是否可以像 python

中那样使用高级 for 循环来做到这一点

这里是方法中的所有代码

public void run(int week, int number)
{
    int cost;
    
    int counter=0;
    HashSet<Integer> use1=new HashSet();
    int numberOfPlayers =number; // obtained from user
    List<HashSet<Integer>> players = new ArrayList<>(numberOfPlayers);
    
    
    List<ArrayList<Integer>> totalEarned= new ArrayList<>();
      for (int i = 0; i < numberOfPlayers; i++) {
        players.add(new HashSet<>());
    }
    for (int i = 0; i < numberOfPlayers; i++) {
        amount.add(new ArrayList<>());
    }
    
    for (HashSet<Integer> player : players) {
        player = userLottery(player);
    }



    System.out.println("");
    do
    {
        week--;
        counter++;
        cost=+2;
        HashSet <Integer>cd=new HashSet();
        comp=computerLottery(comp);

        System.out.println("week : "+counter);
        for (HashSet<Integer> player : players) {
            checkLottery(comp, player);
            
            totalEarned.add(earned(total));
        }
        System.out.println("");
        
        comp.clear();
    }while(week>0);
     System.out.println(earned(total));
}


I'm creating a lottery program using set. the user should be able to enter the number of weeks they would like the lottery to run. i.e if they enter 3 weeks the computer would generate a new set of random number each week to check against those of the player if they get a certain amount of the numbers right they get money. The user is also able to choose how many players

我的主要问题是保持他们赢得的总数。因为它可能不止一个玩家,所以我决定制作一堆数组列表来存放个人分数。因此,由于每周玩家得分都会不同,因此每个数组列表都会跟踪得分。例如,如果玩家在第一周赢得 100 美元。 arraylist 更新为 100,然后如果他们赢了 $20,arraylist 20 将添加到 arraylist,从而保持 运行 总数。

据我所知,您将两组值用于不同的任务。因此我建议用两个循环遍历两者。

你能分享更多你的代码和你想要实现的目标吗?

但我建议用有限的数据是这样的

int numberOfPlayers = 10;
ArrayList<Player> players = new ArrayList<>();

//generate Players
for (int i = 0; i < numberOfPlayers; i++) {
    players.add(new Player());
}
        
//lottery for player
for(int i = 0; i < players.size(); i++){
    Player player = players.get(i);
    player = useLottery(player);
    players.set(i, player);
}

玩家定义为

class Player{
    int score;
    //... other attributes
}