简体   繁体   English

检查 int[] 是否包含一个 int

[英]Check if int[] contains an int

My script is caught in an infinite loop and I cannot figure out why.我的脚本陷入无限循环,我不知道为什么。 I have an array with 40 pawns and need to put these randomly on a board.我有一个包含 40 个棋子的数组,需要将这些棋子随机放在棋盘上。 So, I have a random number that chooses a random pawn from the array, but if the pawn has already been chosen, it has to pick a new random number, but that last part seems to bug for some reason.所以,我有一个随机数从数组中选择一个随机 pawn,但是如果 pawn 已经被选择,它必须选择一个新的随机数,但由于某种原因,最后一部分似乎有问题。 I cannot figure out why.我不明白为什么。

Random rand = new Random();    

int[] availablePawnsArray = {1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12 };
// this array contains 40 integers

int[] chosenPawns = new int[40];
//this array contains the index numbers of already selected pawnsfrom the previous array

int counter = 0;
//counts how many pawns have been selected already    

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 10; j++) {
    //this refers to my board, 40 locations for my 40 pawns    

        int chosenPawn = rand.nextInt(40);
        //a random numder from 0 to 40
        
        boolean found = false;
        //a boolean to say if i have already selected this pawn before or not    

        do {
            for (int n : chosenPawns) {
                if (n == chosenPawn) {
                    found = true;
                    chosenPawn = rand.nextInt(40);
                } else {
                    found = false;
                }
            }
        } while(found == true);    

        board[i][j].rank = availablePawnsArray[chosenPawn];
        chosenPawns[counter] = chosenPawn;
        counter++;
    }
}

You can have two arrays, and second one keep for selected integers, then do loop in second array check if there is any number equal to given one return false or true.您可以有两个数组,第二个保留选定的整数,然后在第二个数组中循环检查是否有任何数字等于给定一个返回 false 或 true。

int [] selectedInts = new int[40];

boolean contains(int num) {
  for (int i = 0 ; i < selectedInts.length; i++) {
    if (i == num) return true;
  }
  return false;
}

Also you can use like你也可以使用像

Arrays.asList().contains(yourInt);

You can simplify this by modifying the random number selection in the same way that one might shuffle a deck of cards.您可以通过修改随机数选择来简化这一过程,就像洗一副纸牌一样。 It is a slight variation of Fisher-Yates.这是Fisher-Yates 的一个轻微变体。 I believe this should always work in linear time.我相信这应该始终在线性时间内起作用。

      Random rand = new Random();
      int[] availablePawnsArray = {
            1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,
            6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12
      };


      int start = 39;
      for (int i = 0; i < 4; i++) {
         for (int k = 0; k < 10; k++) {
            int chosenPawn = rand.nextInt(start + 1);
            // chose the pawn
            board[i][k].rank = availablePawnsArray[chosenPawn];
            // copy the pawn from the end of the list to the 
            // chosen pawn location.
            availablePawnsArray[chosenPawn] = availablePawnsArray[start];
            // update the random number to ignore the last slot
            // in the array (the pawn in that slot has
            // been moved to occupy the chosenPawn's location)
            start--;
         }
      }

      for (int i = 0; i < 4; i++) {
         for (int k = 0; k < 10; k++) {
            System.out.print(board[i][k].rank + " ");
         }
         System.out.println();
      }
   }

Unless you need to implement it as an exercise you could use the built in shuffle method, wrapping your array of available pawns in a list:除非您需要将其作为练习来实现,否则您可以使用内置的shuffle方法,将可用 pawn 的数组包装在一个列表中:

Collections.shuffle(Arrays.asList(availablePawnsArray));
    
for (int i = 0, k = 0; i < 4; i++) 
    for (int j = 0; j < 10; j++, k++) 
        board[i][j].rank = availablePawnsArray[k];

The chosenPawns array should only be searched upto counter. selectedPawns 数组应该只搜索到计数器。 Using repeatedly random to find a free spot when almost all is used, could take several thousands of steps to find the final last open spots.当几乎全部都被使用时,使用重复随机来寻找一个空闲位置,可能需要数千步才能找到最后的最后一个空闲位置。 There is not even a guarantee to find an open spot.甚至不能保证找到空位。

Simply consider the random position as starting point to find the first free pawn, going round-robin from ... 36, 37, 38, 39, 0, 1, ....简单地将随机位置作为起点来找到第一个空闲的棋子,从...... 36, 37, 38, 39, 0, 1, ...开始循环。

int[] pawnsArray = {1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4,
    5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12 };
// this array contains 40 integers

boolean[] pawnsChosen = new boolean[40];
// this array tells wether the i'th pawn was chosen.

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 10; j++) {

        int pawnIndex = rand.nextInt(40);
        while (pawnsChosen[pawnIndex]) {
            pawnIndex = (pawnIndex + 1) % 40;
        }
        pawnsChosen[pawnIndex] = true;

        board[i][j].rank = availablePawnsArray[pawnIndex];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM