Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Search and Array for multiple occurrences of a value

843789Nov 16 2009 — edited Nov 17 2009
Hi,

I am trying to search an array of doubles for a number and report the index location(s) of a number. I.e. -allow for repetition. Search and report all index[i] where the number is contained in the array. For example I have
double[] myInputArray = new double[5];
double[] myInputArray = { 1, 2, 3, 3, 5 }
I know how to search through the array ONCE and return the first occurrence of a number:
public double GetIndexOf(double Number) {
    for (int i=0; i<myInputArray.length; i++) {
    	if (myInputArray[i] == Number){ 
    		return(i); 
    	}
    }
    return(-1);
  }
How do I expand this code to report multiple index[i] locations if the number reoccurs in the array like the number 3 does in my example array above?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2009
Added on Nov 16 2009
12 comments
1,509 views