Check if int[] contains an int

check if int array contains value java
int[] contains c#
check if an array contains a value java
check if a string is in an array of strings java
how to check if an array element exists java
check if json array contains value java
write a java program to test if an array contains a specific value.
c++ program to check if given number is present in an array or not

For some reason, my script is making an infinite loop, I think, and I cannot figure out why. I have an array with 40 pawns and need to put these randomly on a board. So I have a random number that choose a random pawn from the array, but if the pawn has already been chose, it has to pick a new random number, but that last part seems to bug for some reason. 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.

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

Checking if an array contains certain integers, Arrays.asList(perm).contains(num). from How can I test if an array contains a certain value? for (int i = 0; i < perm.length; i++). this is not enough� The following code shows how to check if an int array contains an element. Example using System; / * w w w . j a v a 2 s . c o m * / using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{ public static void Main(){ int [] numbers = { 2, 6, 1, 56, 102 }; Console.WriteLine( "Is

The chosenPawns array should only be searched upto counter. 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, ....

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

Check if a value is present in an Array in Java, private static void check( int [] arr, int toCheckValue) Using List.contains() Method: List contains() method in Java is used for checking if the� Nice question. So what we have to do here, is to go through all the numbers of the array, and check each one to see if they are equal to 20. We can do this with a loop.

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

Java Program to Check if An Array Contains a Given , Example 1: Check if Int Array contains a given value. public class Contains { public static void main(String[] args) { int[] num = {1, 2, 3, 4, 5}; int toFind = 3;� Curious if there is a better way to write a repository method that receives argument List<int> and checks if the database Contains an int?. public List<int>; GetNumbersByNumbers(IEnumer

Check If Java Array Contains A Certain Value, public class Test { public static boolean contains(int[] arr, int item) { for (int n : arr) { if (item == n) { return true; } } return false; } public static void� Here is a routine that will work detect zeros in integers. To make it work with any representation (decimal, hex, octal, binary), you need to pass in the base as a parameter.

Java Program to Check if An Array Contains a Given , Following is a Java program to check if An Array Contains a Given Value. args[] ){ int[] myArray = {55, 45, 69, 44}; int num = 55; for(int i = 0;� Find the numbers from 1 to N that contains exactly k non-zero digits; Count of subarrays which start and end with the same element; Expected Number of Trials to get N Consecutive Heads; Minimum number of operations to convert array A to array B by adding an integer into a subarray

How to Check if an Array Contains a Value in Java Efficiently?, public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays.binarySearch(arr, targetValue); if(a > 0) return true; else return false;� I want to check if this Enum contain a number I give. For example: When I give 4, Enum contain that, So I want to return True, If I give 7, There isn't 7 in this Enum, So it returns False. I tried Enum.IsDefine but it only check the String value.

Comments
  • You are overriding found so it will take its last value
  • What happens, if every pawn is chosen? For every number nextInt provides, there will be a chosenPawn and found will be true, thus the loop never exits.
  • Just a small remark: don't do while(found == true). In fact, found is already a boolean. It should only be while(found).
  • When you do int[] chosenPawns = new int[40]; it's only creating this array of int. You need to fill it.
  • Provided an answer that randomizes your pawn placement in linear time and does not require keeping track of indices.