0

For example I have JSONArray of strings:

["hello", "dear", "community"]

and I want to get index of "dear". How would I do that?

I am iterating over an array and want to check is array element in my JSONArray and get its index.

5

1 Answer 1

5

There is no direct way of getting that. Unless you loop through the objects and find the index for the matching string.

for(int i=0;i<arr.length();i++) {
            if("dear".equals(arr.get(i))) {
                return i;
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

I am iterating over an array and want to check is array element in my JSONArray and get its index.
@onskulis The code Hirak provided does exactly that.
@Zeeker sorry I mean fast enumeration.
@Hirak thank you for solution, sorry for misunderstanding. It works perfectly, just needed to use it as a function.

Your Answer

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