indexOf int数组元素

时间:2014-02-20 09:15:35

标签: java

我有这个代码是在indexOf中查找array 3个元素,所以我使用了java.util ..等等,但它给了我那里我错误的结果是-1 ,如何避免这个错误的答案,还有另一种方法来编写这段代码

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

4 个答案:

答案 0 :(得分:2)

你必须使用“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)]);

(编辑 - 02.03.2014)

嗨,我的第一个回答是错误的,请检查新的:

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

答案 1 :(得分:2)

我不确定我是否得到了答案,但尝试将代码分解为可读行。原始数组应为Integer[]而不是int[]

请注意,Arrays.asList(int[])会创建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);
    }

这可能会有所帮助。

答案 2 :(得分:1)

问题在于int[]数组本身,因为当您使用任何基本数组并将其转换为List using Arrays.asList()时,它将返回List<int[]>而非List<Integer>因此问题发生用你的代码。解决创建array of Integer (Wrapper class)

Integer[] array  = {1,0,1,0,0,1,1,0,1,1};

答案 3 :(得分:1)

为什么你的代码失败了:

这是相关的: Arrays.asList() not working as it should?

您在Arrays.asList上致电int[]。这不会产生预期的结果,因为Arrays.asList用于处理Object的数组,而不是基元。结果是一个元素的列表,该元素是您的int[]数组(因为基元数组 一个Object,就像任何其他数组一样。)

解决方案:

  • 您已经知道自己的索引了。你正在设置它。无需查阅。将其保存在变量中。我喜欢短代码,但有时短代码更糟,运行速度慢于稍长的代码。 NBD,如果你不适合整个程序在一行。 -OR -
  • 如果您仍然认为需要索引查找,请使用整数对象数组(Integer[])。
相关问题