@@ -173,9 +173,7 @@ public JSONArray(Collection<?> collection) {
173173 this .myArrayList = new ArrayList <Object >();
174174 } else {
175175 this .myArrayList = new ArrayList <Object >(collection .size ());
176- for (Object o : collection ){
177- this .myArrayList .add (JSONObject .wrap (o ));
178- }
176+ this .addAll (collection );
179177 }
180178 }
181179
@@ -193,16 +191,7 @@ public JSONArray(Collection<?> collection) {
193191 */
194192 public JSONArray (Object array ) throws JSONException {
195193 this ();
196- if (array .getClass ().isArray ()) {
197- int length = Array .getLength (array );
198- this .myArrayList .ensureCapacity (length );
199- for (int i = 0 ; i < length ; i += 1 ) {
200- this .put (JSONObject .wrap (Array .get (array , i )));
201- }
202- } else {
203- throw new JSONException (
204- "JSONArray initial value should be a string or collection or array." );
205- }
194+ this .addAll (array );
206195 }
207196
208197 /**
@@ -1174,6 +1163,36 @@ public JSONArray put(int index, Object value) throws JSONException {
11741163 }
11751164 return this .put (value );
11761165 }
1166+
1167+ /**
1168+ * Put or replace a collection's elements in the JSONArray.
1169+ *
1170+ * @param collection
1171+ * A Collection.
1172+ * @return this.
1173+ */
1174+ public JSONArray putAll (Collection <?> collection ) {
1175+ this .addAll (collection );
1176+ return this ;
1177+ }
1178+
1179+ /**
1180+ * Put or replace an array's elements in the JSONArray.
1181+ *
1182+ * @param array
1183+ * Array. If the parameter passed is null, or not an array, an
1184+ * exception will be thrown.
1185+ * @return this.
1186+ *
1187+ * @throws JSONException
1188+ * If not an array or if an array value is non-finite number.
1189+ * @throws NullPointerException
1190+ * Thrown if the array parameter is null.
1191+ */
1192+ public JSONArray putAll (Object array ) throws JSONException {
1193+ this .addAll (array );
1194+ return this ;
1195+ }
11771196
11781197 /**
11791198 * Creates a JSONPointer using an initialization string and tries to
@@ -1500,6 +1519,45 @@ public List<Object> toList() {
15001519 public boolean isEmpty () {
15011520 return this .myArrayList .isEmpty ();
15021521 }
1522+
1523+
1524+ /**
1525+ * Add a collection's elements to the JSONArray.
1526+ *
1527+ * @param collection
1528+ * A Collection.
1529+ */
1530+ private void addAll (Collection <?> collection ) {
1531+ this .myArrayList .ensureCapacity (this .myArrayList .size () + collection .size ());
1532+ for (Object o : collection ){
1533+ this .myArrayList .add (JSONObject .wrap (o ));
1534+ }
1535+ }
1536+
1537+ /**
1538+ * Add an array's elements to the JSONArray.
1539+ *
1540+ * @param array
1541+ * Array. If the parameter passed is null, or not an array, an
1542+ * exception will be thrown.
1543+ *
1544+ * @throws JSONException
1545+ * If not an array or if an array value is non-finite number.
1546+ * @throws NullPointerException
1547+ * Thrown if the array parameter is null.
1548+ */
1549+ private void addAll (Object array ) throws JSONException {
1550+ if (array .getClass ().isArray ()) {
1551+ int length = Array .getLength (array );
1552+ this .myArrayList .ensureCapacity (this .myArrayList .size () + length );
1553+ for (int i = 0 ; i < length ; i += 1 ) {
1554+ this .put (JSONObject .wrap (Array .get (array , i )));
1555+ }
1556+ } else {
1557+ throw new JSONException (
1558+ "JSONArray initial value should be a string or collection or array." );
1559+ }
1560+ }
15031561
15041562 /**
15051563 * Create a new JSONException in a common format for incorrect conversions.
0 commit comments