+ 6

I want to know how to find the index number (position) of certain element of an array in java.

int [] i = {6,5,8,9,2,3} I just want to know how can I know the index number of any element of i like 9.

23rd Aug 2017, 10:17 AM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
4 Respostas
+ 5
public int getArrayIndex(int[] arr,int value) { int k=0; for(int i=0;i<arr.length;i++) { if(arr[i]==value) { k=i; break; } } return k; } //Call this method and pass two parameters that are Array and value int indexNum = getArrayIndex(i,9); //here , u hv to pass these parameters to know the index of "9" in array "i"
24th Aug 2017, 6:31 AM
Changed
Changed - avatar
+ 6
I know but I want to know the method of how I can get the index number of any element of an array by using any method.
23rd Aug 2017, 1:15 PM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
+ 6
thanks I think this is what I want
24th Aug 2017, 7:33 AM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
+ 4
it will be 1 less than the actual position bcz the in java we start counting index from 0 so i [0] is 6 i [1] is 5 i [3] is 9 //ie index of 9 is 3 in the above code
23rd Aug 2017, 11:23 AM
Changed
Changed - avatar