Interface JSONArray
- All Superinterfaces:
Formattable, Iterable<JSONValue>, JSONValue
- All Known Implementing Classes:
JSONArrayImpl
The definition of a JSON array.
Basically, aa JSON array is dense, meaning that an array with the
maximum index i has exactly i + 1 elements –
although some of these elements could be
null.
The add(T) methods will add the given value to the end of the
array, while the add(int,T) methods will insert the given value at
the position identified by the given index; the already existing
values at that position and above will be moved to the next higher
position. If the index is equal to the
size
of the array, the value will be added to the end of the array. That means
that add( value ) and add( size(), value ) are equivalent.
A value for index that is greater than the size of the array (or
less than 0) will cause an
IndexOutOfBoundsException
to be thrown.
The set(int,T) methods will replace the value at the given
index; this means, that after a call to
set(int,JSONValue)
the
size
of this array remains the same, while a call to
add(JSONValue)
or
add(int,JSONValue)
increases the array size by one.
Obviously must the value for the index argument of a
set() match an existing array position; otherwise an
IndexOutOfBoundsException
is thrown.
An instance of JSONArray is not thread-safe.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: JSONArray.java 1258 2026-06-04 18:33:06Z tquadrat $
- Since:
- 0.25.0
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.jsonbuilder.JSONArray"
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidadd(boolean value) Appends the given value as a new element to the end of this array.voidadd(double value) Appends the given value as a new element to the end of this array.voidadd(float value) Appends the given value as a new element to the end of this array.voidadd(int value) Appends the given value as a new element to the end of this array.default voidadd(int index, boolean value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidadd(int index, double value) Inserts the given value as a new element to the given postion of this array and moves the other elements to a by one higher index.voidadd(int index, float value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidadd(int index, int value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidadd(int index, long value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidadd(int index, BigDecimal value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidadd(int index, BigInteger value) Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.voidInserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.<T extends Dimension>
voidadd(int index, DimensionedValue<T> value, T targetUnit) Inserts the given value as a new element to the given postion of this array and moves the other elements to a by one higher index.voidadd(long value) Appends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.voidadd(BigDecimal value) Appends the given value as a new element to the end of this array.voidadd(BigInteger value) Appends the given value as a new element to the end of this array.voidAppends the given value as a new element to the end of this array.<T extends Dimension>
voidadd(DimensionedValue<T> value, T targetUnit) Appends the given value as a new element to the end of this array.booleanAppends the given array to this one.addArray()Adds a new empty instance ofJSONArrayto this array and returns that.addArray(int index) Adds a new empty instance ofJSONArrayto this array at the given index and returns that.Adds a new empty instance ofJSONObjectto this array and returns that.addObject(int index) Adds a new empty instance ofJSONObjectto this array at the given index and returns that.get(int index) Returns the value of the element at the specied index in this array.default JSONArraygetArray(int index) Convenience method that returns the value with the specified index as aJSONArray.default BigDecimalgetBigDecimal(int index) Convenience method that returns the value with the specified index as aBigDecimal.default BigIntegergetBigInteger(int index) Convenience method that returns the value with the specified index as aBigInteger.default booleangetBoolean(int index) Convenience method that returns the value with the specified index as aboolean.default doublegetDouble(int index) Convenience method that returns the value with the specified index as adouble.default floatgetFloat(int index) Convenience method that returns the value with the specified index as afloat.default intgetInt(int index) Convenience method that returns the value with the specified index as aint.default longgetLong(int index) Convenience method that returns the value with the specified index as along.default JSONObjectgetObject(int index) Convenience method that returns the value with the specified index as aJSONObject.default StringgetString(int index) Convenience method that returns the value with the specified index as aString.booleanisEmpty()Checks whether this array has elements.voidremove(int index) Removes the element with the specified index from this array.voidRemoves the first occurrence of the specified element from this array, if it is present.default voidset(int index, boolean value) Sets the value of the element with the specified index to the JSON representation of the specifiedbooleanvalue.voidset(int index, double value) Sets the value of the element with the specified index to the JSON representation of the specifieddoublevalue.voidset(int index, float value) Sets the value of the element with the specified index to the JSON representation of the specifiedfloatvalue.voidset(int index, int value) Sets the value of the element with the specified index to the JSON representation of the specifiedintvalue.voidset(int index, long value) Sets the value of the element with the specified index to the JSON representation of the specifiedlongvalue.voidSets the value of the element with the specified index to the JSON representation of the specifiedDoublevalue.voidSets the value of the element with the specified index to the JSON representation of the specifiedFloatvalue.voidSets the value of the element with the specified index to the JSON representation of the specifiedIntegervalue.voidSets the value of the element with the specified index to the JSON representation of the specifiedLongvalue.voidSets the value of the element with the specified index to the JSON representation of the specifiedStringvalue.voidset(int index, BigDecimal value) Sets the value of the element with the specified index to the JSON representation of the specifiedBigDecimalvalue.voidset(int index, BigInteger value) Sets the value of the element with the specified index to the JSON representation of the specifiedBigIntegervalue.voidSets the value of the element with the specified index to the givenJSONValueinstance.<T extends Dimension>
voidset(int index, DimensionedValue<T> value, T targetUnit) Sets the value of the element with the specified index to the JSON representation of the specifiedDimensionedValuevalue.setArray(int index) Sets a new empty instance ofJSONArrayto this array at the given index and returns that.setObject(int index) Sets a new empty instance ofJSONObjectto this array at the given index and returns that.intsize()Returns the number of elements for this array.Methods inherited from interface Iterable
forEach, iterator, spliterator
-
Method Details
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
Appends the given value as a new element to the end of this array.
- Type Parameters:
T- The type of the dimension for the value.- Parameters:
value- The value.targetUnit- The dimension for the output.- See Also:
-
add
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
-
add
-
add
Appends the given value as a new element to the end of this array.
- Parameters:
value- The value to add.
-
add
-
add
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
<T extends Dimension> void add(int index, DimensionedValue<T> value, T targetUnit) throws IndexOutOfBoundsException Inserts the given value as a new element to the given postion of this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Type Parameters:
T- The type of the dimension for the value.- Parameters:
index- The index.value- The value.targetUnit- The dimension for the output.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.- See Also:
-
add
Inserts the given value as a new element to the given postion of this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
add
Inserts the given value as a new element to the given postion this array and moves the other elements to a by one higher index.
If
indexis equals to the size of the array, the element is appended to the end of the array.- Parameters:
index- The index.value- The value to add.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
addAll
-
addArray
-
addArray
Adds a new empty instance of
JSONArrayto this array at the given index and returns that.- Parameters:
index- The index.- Returns:
- The freshly created
JSONArray. - Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
addObject
Adds a new empty instance of
JSONObjectto this array and returns that.- Returns:
- The freshly created
JSONObject.
-
addObject
Adds a new empty instance of
JSONObjectto this array at the given index and returns that.- Parameters:
index- The index.- Returns:
- The freshly created
JSONObject. - Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than the size of the array.
-
get
Returns the value of the element at the specied index in this array.
- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The element; will never be
null. - Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getArray
Convenience method that returns the value with the specified index as a
JSONArray.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a JSON Array.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getBigDecimal
default BigDecimal getBigDecimal(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
BigDecimal.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validBigDecimal.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getBigInteger
default BigInteger getBigInteger(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
BigInteger.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validBigInteger.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getBoolean
Convenience method that returns the value with the specified index as a
boolean.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a boolean.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getDouble
default double getDouble(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
double.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validdouble.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getFloat
default float getFloat(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
float.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validfloat.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getInt
default int getInt(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
int.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validint.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getLong
default long getLong(int index) throws IllegalStateException, IndexOutOfBoundsException, NumberFormatException Convenience method that returns the value with the specified index as a
long.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a number.NumberFormatException- The element is a number, but cannot be parsed to a validlong.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getObject
Convenience method that returns the value with the specified index as a
JSONObject.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a JSON Object.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
getString
Convenience method that returns the value with the specified index as a
String.- Parameters:
index- The index of the element whose value is to be returned.- Returns:
- The value of the element with the specified index.
- Throws:
IllegalStateException- The element is not a String.IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
isEmpty
boolean isEmpty()Checks whether this array has elements.- Returns:
trueif the object does not have any elements,falseotherwise.
-
remove
Removes the element with the specified index from this array.
- Parameters:
index- The index of the element to remove.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
remove
Removes the first occurrence of the specified element from this array, if it is present. If this list does not contain the element, it remains unchanged.
More formally, this method removes the element with the lowest index
isuch thatObjects.equals(o, get(i))(if such an element exists).- Parameters:
element- The element to remove.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
BigDecimalvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
BigIntegervalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
booleanvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
<T extends Dimension> void set(int index, DimensionedValue<T> value, T targetUnit) throws IndexOutOfBoundsException Sets the value of the element with the specified index to the JSON representation of the specified
DimensionedValuevalue.- Type Parameters:
T- The type of the dimension for the value.- Parameters:
index- The index.value- The value.targetUnit- The dimension for the output.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.- See Also:
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
doublevalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
Doublevalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
intvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
floatvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
Floatvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
Integervalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the given
JSONValueinstance.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
longvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
Longvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
set
Sets the value of the element with the specified index to the JSON representation of the specified
Stringvalue.- Parameters:
index- The index of the element to replace.value- The value to set to the member.- Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
setArray
Sets a new empty instance of
JSONArrayto this array at the given index and returns that.- Parameters:
index- The index.- Returns:
- The freshly created
JSONArray. - Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
setObject
Sets a new empty instance of
JSONObjectto this array at the given index and returns that.- Parameters:
index- The index.- Returns:
- The freshly created
JSONObject. - Throws:
IndexOutOfBoundsException- The index is less than 0 or greater than or equal to the size of the array.
-
size
int size()Returns the number of elements for this array.- Returns:
- The number of members in this object.
-
