You have to use wrapper class of "int"
SecureRandom randomNumbers = new SecureRandom();
Integer array[] = { 1, 0, 1, 0, 0, 1, 1, 0, 1, 1 };
for (int counter = 0; counter < 3; counter++)
System.out.printf("%5d%8d\n", java.util.Arrays.asList(array).indexOf(array[randomNumbers.nextInt(10)]), array[randomNumbers.nextInt(10)]);
(Edit - 02.03.2014)
Hi, my first answer was wrong, check the new one:
SecureRandom randomNumbers = new SecureRandom();
/**
* I don't know why but when you create an array with only primitive
* contained "unnamed block" like "Integer array[] = {1,0,1}". JVM
* associate same referance variables with same values like "Integer
* array[] = {@34, @554, @34}". That is why i use "new Integer"
* constractor for each integer as follows:
*/
Integer array[] = new Integer[] { new Integer(1), new Integer(0),
new Integer(1), new Integer(0), new Integer(0), new Integer(1),
new Integer(1), new Integer(0), new Integer(1), new Integer(1) };
int ran, i;
for (int counter = 0; counter < 3; counter++) {
ran = randomNumbers.nextInt(10);
i = 0;
/**
* "List.indexOf" method uses "object1.equals(object2)" method, this
* method compares "values" of wrapper classes, but in your case
* we have to compare referances, so with nested "for" loops we
* check that:
*/
for (Integer integer : array) {
if (integer == array[ran]) { // "==" operator checks referances is same
System.out.printf("%5d%8d\n", i, ran);
break;
}
i++;
}
}