Question:
Is there any Java API function to find the index of specified element in an array?
yuku
2007-02-21 11:59:28 UTC
For example the array int[] a = {10, 20, 30, 40}
Then I want to find where is 30 in that array. The function should return 2 (index 2 of the array).
Surely I can loop through the array and check the equality. But it is such a standard function, is there any function available in standard J2SE API that does that thing?
Three answers:
ravikumar_g26
2007-02-21 12:42:36 UTC
Hi, there are no methods to give you the result in single line of code. If its a String array, we can convert it to List object and then get the index. see below.



String[] s ={"10", "20", "30", "40"};

List list = Arrays.asList(s);

System.out.println("The index is "+ list.indexOf("40"));



So even to use this, there is no easy way to convert/cast int array to String array. So You need to manipulate manually.
anonymous
2007-02-21 12:04:55 UTC
well I suppose it's a pretty valid question, but really you would have wasted less time not posting question lol



I know of no such function, but I know only a small amount so that doesn't count for much I guess
anonymous
2016-12-18 13:06:22 UTC
int SumofM(int[] array,int pos,int M) { if ( M > array.length){ return 0; } if(pos==(M-one million)){ return array[pos]; } return array[pos] + SumofM(array,pos+one million,M); } that's the form you call to compute first 5 components. SumofM(array,0.5)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...