检查int []是否包含int

时间:2019-12-13 13:12:33

标签: java arrays random infinite-loop do-while

出于某种原因,我认为我的脚本正在无限循环,我无法弄清原因。 我有一个带有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++;
    }
}

3 个答案:

答案 0 :(得分:1)

您可以有两个数组,第二个数组保留选定的整数,然后在第二个数组中循环检查是否有等于给定一个的数字返回false或true。

Arrays.asList().contains(yourInt);

您也可以使用like

nums

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以通过修改随机数选择来简化此操作,就像选择随机播放一副纸牌一样。这是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();
      }
   }