1

I am trying to search an array for an int but it only give the location of the first index it finds if the int appears more than once. For example, if the number 2 appears at index 3 and 7. It will say 3 found at location 3 twice instead of saying 2 found at location 3 and 7. How can I get the additional index of the location where the number also appears.

// random_integers is an array of random integers of size 10
Arrays.asList(random_integers);

for (int n : random_integers) {
    if (n == number) {
        System.out.println("Search Value: "
                + number
                + " found at location: "
                + Arrays.asList(random_integers).indexOf(n)
                + " in the unsorted array");
        }
    }

Thank you.

1
  • indexOf method prints first occurrence of object in list , not all. So In one iteration its difficult to get all indexes using arraylist Commented Oct 21, 2015 at 15:56

5 Answers 5

3

That's the behavior of indexOf. Instead, use a traditional for loop and just use the current looping variable

for (int i = 0; i < random_integers.length; i++){
    if (random_integers[i] == number){
        System.out.println("Search Value: " + number + " found at location: " + i);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This happens because List#indexOf() has no idea about your intention to keep last index where item was found recently, so it returns first index every time. You should iterate over array by index, printing it when occurrence found:

for (int i; i < random_number.length; i++) {
    if (random_integers[i] == number) {
        System.out.format("Search Value: %s found at location: %s in the unsorted array", number, i);
    }
}

Comments

0

The indexOf() method returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.

A traditional for loop will work here.

for(int i=0; i < random_integers.length;i++){
    if (random_integers[i] == number){
        System.out.println("number=" + number + " location=" + i);
    }
}

Comments

0

As what all guys said, use traditional for loop instead. If you want to use asList , try the following code:

   int size =  Arrays.asList(a).size();


 for (int i = 0 ; i < size ; i++){

     if (number == Arrays.asList(a).get(i))
 System.out.println("  Search Value: " + number +
            " found at location: " +        i +
            " in the unsorted array");
 }

Comments

0

This code only works, if you are looking to find the current and next index value of a given array, however, if you are looking to find the index of a given value, refer to Michael's answer on this thread.

    int totalLength = random_integers.length;

    for (int i=0; i < totalLength; i++){
        int nextIndex = i+1;
        if (nextIndex == totalLength){
            break;
        }else{
           int currentIndexValue = random_integers[i];
           int nextIndexValue = random_integers[nextIndex];
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.