public class JSONArray extends JSONValue implements Iterable<JSONValue>
Elements can be added using the add(...) methods which accept
instances of JSONValue, strings, primitive numbers, and boolean
values. To replace an element of an array, use the set(int, ...)
methods.
Elements can be accessed by their index using get(int). This class
also supports iterating over the elements in document order using an
iterator() or an enhanced for loop:
for (JsonValue value : jsonArray) {
...
}
An equivalent List can be obtained from the method values().
Note that this class is not thread-safe. If multiple threads
access a JsonArray instance concurrently, while at least one of
these threads modifies the contents of this array, access to the instance
must be synchronized externally. Failure to do so may lead to an inconsistent
state.
This class is not supposed to be extended by clients.
| 构造器和说明 |
|---|
JSONArray()
Creates a new empty JsonArray.
|
JSONArray(JSONArray array)
Creates a new JsonArray with the contents of the specified JSON array.
|
| 限定符和类型 | 方法和说明 |
|---|---|
JSONArray |
add(boolean value)
Appends the JSON representation of the specified
boolean
value to the end of this array. |
JSONArray |
add(double value)
Appends the JSON representation of the specified
double
value to the end of this array. |
JSONArray |
add(float value)
Appends the JSON representation of the specified
float value
to the end of this array. |
JSONArray |
add(int value)
Appends the JSON representation of the specified
int value
to the end of this array. |
JSONArray |
add(JSONValue value)
Appends the specified JSON value to the end of this array.
|
JSONArray |
add(long value)
Appends the JSON representation of the specified
long value
to the end of this array. |
JSONArray |
add(Object value) |
JSONArray |
add(String value)
Appends the JSON representation of the specified string to the end of
this array.
|
JSONArray |
asArray()
Returns this JSON value as
JSONArray, assuming that this value
represents a JSON array. |
boolean |
equals(Object object)
Indicates whether some other object is "equal to" this one according to
the contract specified in
Object.equals(Object). |
JSONValue |
get(int index)
Returns the value of the element at the specified position in this array.
|
int |
hashCode() |
boolean |
isArray()
Detects whether this value represents a JSON array.
|
boolean |
isEmpty()
Returns
true if this array contains no elements. |
Iterator<JSONValue> |
iterator()
Returns an iterator over the values of this array in document order.
|
JSONArray |
remove(int index)
Removes the element at the specified index from this array.
|
JSONArray |
set(int index,
boolean value)
Replaces the element at the specified position in this array with the
JSON representation of the specified
boolean value. |
JSONArray |
set(int index,
double value)
Replaces the element at the specified position in this array with the
JSON representation of the specified
double value. |
JSONArray |
set(int index,
float value)
Replaces the element at the specified position in this array with the
JSON representation of the specified
float value. |
JSONArray |
set(int index,
int value)
Replaces the element at the specified position in this array with the
JSON representation of the specified
int value. |
JSONArray |
set(int index,
JSONValue value)
Replaces the element at the specified position in this array with the
specified JSON value.
|
JSONArray |
set(int index,
long value)
Replaces the element at the specified position in this array with the
JSON representation of the specified
long value. |
JSONArray |
set(int index,
String value)
Replaces the element at the specified position in this array with the
JSON representation of the specified string.
|
int |
size()
Returns the number of elements in this array.
|
static JSONArray |
unmodifiableArray(JSONArray array)
Returns an unmodifiable wrapper for the specified JsonArray.
|
List<JSONValue> |
values()
Returns a list of the values in this array in document order.
|
asBoolean, asDouble, asFloat, asInt, asJSONObject, asLong, asString, isBean, isBoolean, isFalse, isLong, isNull, isNumber, isObject, isString, isTrue, toString, toString, writeTo, writeToforEach, spliteratorpublic JSONArray()
public JSONArray(JSONArray array)
array - the JsonArray to get the initial contents from, must not be
nullpublic static JSONArray unmodifiableArray(JSONArray array)
The returned JsonArray is backed by the given array and reflects
subsequent changes. Attempts to modify the returned JsonArray result in
an UnsupportedOperationException.
array - the JsonArray for which an unmodifiable JsonArray is to be
returnedpublic JSONArray add(int value)
int value
to the end of this array.value - the value to add to the arraypublic JSONArray add(long value)
long value
to the end of this array.value - the value to add to the arraypublic JSONArray add(float value)
float value
to the end of this array.value - the value to add to the arraypublic JSONArray add(double value)
double
value to the end of this array.value - the value to add to the arraypublic JSONArray add(boolean value)
boolean
value to the end of this array.value - the value to add to the arraypublic JSONArray add(String value)
value - the string to add to the arraypublic JSONArray add(JSONValue value)
value - the JsonValue to add to the array, must not be
nullpublic JSONArray set(int index, int value)
int value.index - the index of the array element to replacevalue - the value to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, long value)
long value.index - the index of the array element to replacevalue - the value to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, float value)
float value.index - the index of the array element to replacevalue - the value to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, double value)
double value.index - the index of the array element to replacevalue - the value to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, boolean value)
boolean value.index - the index of the array element to replacevalue - the value to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, String value)
index - the index of the array element to replacevalue - the string to be stored at the specified array positionIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray set(int index, JSONValue value)
index - the index of the array element to replacevalue - the value to be stored at the specified array position, must
not be nullIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic JSONArray remove(int index)
index - the index of the element to removeIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic int size()
public boolean isEmpty()
true if this array contains no elements.true if this array contains no elementspublic JSONValue get(int index)
index - the index of the array element to returnIndexOutOfBoundsException - if the index is out of range, i.e. index < 0
or index >= sizepublic List<JSONValue> values()
public Iterator<JSONValue> iterator()
public boolean isArray()
JSONValueJSONArray.public JSONArray asArray()
JSONValueJSONArray, assuming that this value
represents a JSON array. If this is not the case, an exception is thrown.public boolean equals(Object object)
JSONValueObject.equals(Object).
Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.
Copyright © 2016. All rights reserved.