-1

I have this code is to find the indexOf 3 elements in array, so I used java.util.. etc, but it gives me that there I mistake the result is -1, how to avoid this wrong answer, and is there another way to write this code

int 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)]);
Prasad
  • 1,188
  • 3
  • 11
  • 29
Ashraf
  • 1
  • 1

4 Answers4

2

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++;
    }
}
veysiertekin
  • 1,731
  • 2
  • 15
  • 40
  • because when you pass as "...indexOf(array[randomNumbers.nextInt(10)])" 'int' automacally converted to Integer. This new "Integer" object is different then you add previously. – veysiertekin Feb 20 '14 at 09:25
  • 1
    No, actually, the problem is with calling `Arrays.asList` on an array of primitives (see http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should ) – trutheality Feb 20 '14 at 09:32
  • @trutheality my first answer was wrong; check the new one! – veysiertekin Mar 02 '14 at 21:12
2

I'm not sure if I've got the answer, but try breaking up your code into readable lines. And your original array should be Integer[] instead of int[].

Note that Arrays.asList(int[]) creates a List<int[]>.

    Random randomNumbers = new Random();

    Integer array [] = {1,0,1,0,0,1,1,0,1,1};
    for(int counter = 0 ; counter < 3; counter++)
    {
        int randomItemFromArray = array[randomNumbers.nextInt(10)];
        List<Integer> listOfInts = Arrays.asList(array);
        int indexOfRandomItem = listOfInts.indexOf(randomItemFromArray);

        System.out.printf("%5d%8d\n", indexOfRandomItem , randomItemFromArray);
    }

This might help.

vegemite4me
  • 6,621
  • 5
  • 53
  • 79
1

Problem is with the int[] array itself because when you use any primitive array and convert it to a List using Arrays.asList() it will return a List<int[]> not List<Integer> hence the issue occurring with your code. to resolve create array of Integer (Wrapper class)

Integer[] array  = {1,0,1,0,0,1,1,0,1,1};
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
1

Why your code fails:

This is relevant: Arrays.asList() not working as it should?

You call Arrays.asList on an int[]. This does not produce the expected result because Arrays.asList is made to work with an array of Objects, not primitives. The result is a list of one element, that element is your int[] array (because an array of primitives is an Object, like any other array).

The solutions:

  • You already know your index. You're setting it. No need to look it up. Save it in a variable. I love short code, but sometimes short code is worse and runs slower than slightly longer code. NBD if you didn't fit the entire program on one line. -OR-
  • If you still think you need an index lookup, use an array of integer objects (Integer[]).
Community
  • 1
  • 1
trutheality
  • 23,114
  • 6
  • 54
  • 68