繁体   English   中英

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

[英]Check if int[] contains an int

我的脚本陷入无限循环,我不知道为什么。 我有一个包含 40 个棋子的数组,需要将这些棋子随机放在棋盘上。 所以,我有一个随机数从数组中选择一个随机 pawn,但是如果 pawn 已经被选择,它必须选择一个新的随机数,但由于某种原因,最后一部分似乎有问题。 我不明白为什么。

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

您可以有两个数组,第二个保留选定的整数,然后在第二个数组中循环检查是否有任何数字等于给定一个返回 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;
}

你也可以使用像

Arrays.asList().contains(yourInt);

您可以通过修改随机数选择来简化这一过程,就像洗一副纸牌一样。 这是Fisher-Yates 的一个轻微变体。 我相信这应该始终在线性时间内起作用。

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

除非您需要将其作为练习来实现,否则您可以使用内置的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];

selectedPawns 数组应该只搜索到计数器。 当几乎全部都被使用时,使用重复随机来寻找一个空闲位置,可能需要数千步才能找到最后的最后一个空闲位置。 甚至不能保证找到空位。

简单地将随机位置作为起点来找到第一个空闲的棋子,从...... 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