Checking if an array contains specific integers

I am making an array with a length of 10 integers, so each place 0-9 contains a different integer 0-9.

I am having trouble figuring out how to check if an array already contains a certain number, and if so, it will regenerate a new one.

So far I:

for (int i = 0; i < perm.length; i++) { int num = (int) (Math.random() * 9); int [] perm[i] = num; } 
0
java arrays integer
Dec 09
source share
2 answers
 Arrays.asList(perm).contains(num) 

from How to check if an array contains a specific value?

 for (int i = 0; i < perm.length; i++) 

this is not enough for loops like this, if a collision occurs, some slots would not be initialized.

In general , for this task it is better to initialize the array with the values ​​in order, and then shuffle it using random permutation indices

+5
Dec 09 '12 at 16:12
source share

here is the full answer

 int[] array = {3,9, 6, 5, 5, 5, 9, 10, 6, 9,9}; SortedSet<Integer> s = new TreeSet(); int numberToCheck=61; for (int i = 0; i < array.length; i++) { s.add(array[i]); } System.out.println("Number contains:"+!(s.add(numberToCheck))); System.out.println("Sorted list:"); System.out.print(s); 
0
May 20 '15 at 19:15
source share



All Articles