java – Deleting JSONArray Element

Question:

In my Android app comes JSONArray . It looks something like this:

[19,145,51]

Under certain circumstances, I have to delete one of the array elements. Now I do it like this:

jsonAr.remove(i);

But the remove method works starting from API 19. I need to do it at 17. How can I remove an element so that API 17 also understands what's what?

Answer:

If you do not need to leave a link to the original array and it is permissible to create a new one, then like this:

public static JSONArray remove(final JSONArray from, final int index) throws JSONException {
    final JSONArray res = new JSONArray();
    for (int i = 0, count = from.length(); i < count; i++) {
        if(index != i)
            res.put(from.get(i));
    }

    return res;
}
Scroll to Top
AllEscort