Development for stleary#516 completed with rebased repository

- Introduced JSONObject(int) constructor.
   - int > Initial capacity of the underlying data structure

- Test for new introduced JSONArray(int) constructor.
   1. Checked with input parameter: 0
   2. Checked with input parameter: positive number
   3. Checked with positive number input parameter and compared length
   4. If input parameter is negative number JSONException is thrown:
         JSONArray initial capacity cannot be negative.
This commit is contained in:
vivek
2020-06-03 11:46:48 +02:00
parent 19bb6fd606
commit d088cf014e
2 changed files with 37 additions and 0 deletions

View File

@@ -205,6 +205,22 @@ public class JSONArray implements Iterable<Object> {
}
}
/**
* Construct a JSONArray with the specified initial capacity.
*
* @param initialCapacity
* the initial capacity of the JSONArray.
* @throws JSONException
* If the initial capacity is negative.
*/
public JSONArray(int initialCapacity) throws JSONException {
if (initialCapacity < 0) {
throw new JSONException(
"JSONArray initial capacity cannot be negative.");
}
this.myArrayList = new ArrayList<Object>(initialCapacity);
}
@Override
public Iterator<Object> iterator() {
return this.myArrayList.iterator();