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.
indexOfmethod prints first occurrence of object in list , not all. So In one iteration its difficult to get all indexes using arraylist