Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了JSONArray中的put(int index, X value)深入大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
JSONArray中的put(int index,X 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,"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的数组属性。 以上是今天工作遇到的一个小问题,整理一下

大佬总结

以上是大佬教程为你收集整理的JSONArray中的put(int index, X value)深入全部内容,希望文章能够帮你解决JSONArray中的put(int index, X value)深入所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。