JSONArray中的put(int index, X value)深入
2013-06-26 19:27
411 查看
JSONArray中的put(int index, X value)
今天有意间看了一下put(int index, X value),发现了原来这个并不是那么简单,也就是说put(1,"First")和put(1000,"One Thousand")是代价不一样的。
深入代码一下:
其中最重要的是这句话
其中values是一个ArrayList
所以put(1,"First")和put(1000,"One Thousand")是代价不一样的。
但是为什么会增加这么多key-value呢(如其中的2,3。。。999)原因就是values是ArrayList,下面是ArrayList.set(index,value)方法
以上是今天工作遇到的一个小问题,整理一下。
今天有意间看了一下put(int index, X value),发现了原来这个并不是那么简单,也就是说put(1,"First")和put(1000,"One Thousand")是代价不一样的。
深入代码一下:
public JSONArray put(int index, Object value) throws JSONException { if (value instanceof Number) { // deviate from the original by checking all Numbers, not just floats & doubles JSON.checkDouble(((Number) value).doubleValue()); } while (values.size() <= index) { values.add(null); } values.set(index, value); return this; }
其中最重要的是这句话
while (values.size() <= index) { values.add(null); }
其中values是一个ArrayList
public JSONArray() { values = new ArrayList<Object>(); }
所以put(1,"First")和put(1000,"One Thousand")是代价不一样的。
但是为什么会增加这么多key-value呢(如其中的2,3。。。999)原因就是values是ArrayList,下面是ArrayList.set(index,value)方法
@Override public E set(int index, E object) { Object[] a = array; if (index >= size) { throwIndexOutOfBoundsException(index, size); } @SuppressWarnings("unchecked") E result = (E) a[index]; a[index] = object; return result;所以现在可以知道,上面疑问的原因了。这就是ArrayList的数组属性。
以上是今天工作遇到的一个小问题,整理一下。
相关文章推荐
- JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
- JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
- JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
- JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
- iOS中上传Array形式的json数据
- String与JSON的转换和Array转String
- Android开发将List转化为JsonArray和JsonObject
- .Net深入实战系列—JSON序列化那点事儿
- android 读取json数据(遍历JSONObject和JSONArray
- 深入分析python数据挖掘 Json结构分析
- JSONArray解数组,三秒更新
- 使用json-lib将Java对象转SONObject,Java集合转JSONArray
- jquery操作JSONArray
- Android中对JSONArray数组的指定项进行删除,更新。
- JSONObject与JSONArray
- php curl获取到json对象并转成数组array的方法
- json学习之三:JSONArray的应用
- 创建JSONArray的常用四种方法
- 判断字符串是JSONObject还是JSONArray
- PHP 将json的stdClass Object转成数组array