diff options
-rw-r--r-- | json/src/main/java/org/json/JSONArray.java | 11 | ||||
-rw-r--r-- | json/src/test/java/org/json/JSONArrayTest.java | 21 |
2 files changed, 28 insertions, 4 deletions
diff --git a/json/src/main/java/org/json/JSONArray.java b/json/src/main/java/org/json/JSONArray.java index a885125..8b7d0af 100644 --- a/json/src/main/java/org/json/JSONArray.java +++ b/json/src/main/java/org/json/JSONArray.java @@ -276,6 +276,17 @@ public class JSONArray { } /** + * Removes and returns the value at {@code index}, or null if the array has no value + * at {@code index}. + */ + public Object remove(int index) { + if (index < 0 || index >= values.size()) { + return null; + } + return values.remove(index); + } + + /** * Returns the value at {@code index} if it exists and is a boolean or can * be coerced to a boolean. * diff --git a/json/src/test/java/org/json/JSONArrayTest.java b/json/src/test/java/org/json/JSONArrayTest.java index 1e72af2..52767db 100644 --- a/json/src/test/java/org/json/JSONArrayTest.java +++ b/json/src/test/java/org/json/JSONArrayTest.java @@ -17,6 +17,7 @@ package org.json; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import junit.framework.TestCase; @@ -136,7 +137,7 @@ public class JSONArrayTest extends TestCase { public void testNulls() throws JSONException { JSONArray array = new JSONArray(); - array.put(3, null); + array.put(3, (Collection) null); array.put(0, JSONObject.NULL); assertEquals(4, array.length()); assertEquals("[null,null,null,null]", array.toString()); @@ -178,7 +179,7 @@ public class JSONArrayTest extends TestCase { */ public void testParseNullYieldsJSONObjectNull() throws JSONException { JSONArray array = new JSONArray("[\"null\",null]"); - array.put(null); + array.put((Collection) null); assertEquals("null", array.get(0)); assertEquals(JSONObject.NULL, array.get(1)); try { @@ -296,7 +297,7 @@ public class JSONArrayTest extends TestCase { public void testJoin() throws JSONException { JSONArray array = new JSONArray(); - array.put(null); + array.put((Collection) null); assertEquals("null", array.join(" & ")); array.put("\""); assertEquals("null & \"\\\"\"", array.join(" & ")); @@ -345,7 +346,7 @@ public class JSONArrayTest extends TestCase { JSONArray values = new JSONArray(); values.put(5.5d); - values.put(null); + values.put((Collection) null); // null values are stripped! JSONObject object = values.toJSONObject(keys); @@ -538,4 +539,16 @@ public class JSONArrayTest extends TestCase { } catch (JSONException e) { } } + + public void test_remove() throws Exception { + JSONArray a = new JSONArray(); + assertEquals(null, a.remove(-1)); + assertEquals(null, a.remove(0)); + + a.put("hello"); + assertEquals(null, a.remove(-1)); + assertEquals(null, a.remove(1)); + assertEquals("hello", a.remove(0)); + assertEquals(null, a.remove(0)); + } } |