zoukankan      html  css  js  c++  java
  • Java JSONArray for循环 remove成员的一个好算法

    开发者技术变现资源聚集地

    https://www.baiydu.com

    好久没写日志了,今天来写一个,JSONArry fro循环判断value,移除不需要的,不需再将JSONArry再次转换成 map或list。

    JSONArray移除成员只能通过for遍历取成员,判断后remove(index),这样很明显是不行的,除非移除的成员只有1个,否则再你移除后for循环的遍历次数就被打乱了。你就会移除到不需要移除的成员。处理这个问题的方式其实很多的,比如创建list范型map将json遍历后一个一个弄到他们里面去......

        我这里说的方式不需要创建LIST,MAP 直接就是用 remove和原本的JSONArry实现,并且效率应该是最高的。

        

    parameterArray=“[{"boundingBox":"29,28,401,29,399,85,27,84","words":[{"boundingBox":"29,28,70,29,68,85,27,84","word":"好"},{"boundingBox":"84,29,111,29,109,85,82,85","word":"友"},{"boundingBox":"125,30,401,29,399,85,123,86","word":"137*8288"}],"text":"好友137*8288"},{"boundingBox":"799,25,1018,29,1016,91,797,87","words":[{"boundingBox":"799,25,931,25,929,87,797,87","word":"10,090"},{"boundingBox":"945,25,960,25,958,87,943,87","word":"金"},{"boundingBox":"975,25,1018,29,1016,91,973,87","word":"币"}],"text":"10,090金币"},{"boundingBox":"25,123,279,127,277,174,23,170","words":[{"boundingBox":"36,124,71,127,69,174,34,171","word":"02"},{"boundingBox":"82,127,94,128,92,175,80,174","word":"月"},{"boundingBox":"106,128,140,128,138,175,104,175","word":"21"},{"boundingBox":"152,129,164,128,162,175,150,176","word":"日"},{"boundingBox":"187,128,279,127,277,174,185,175","word":"18:50"}],"text":"02月21日 18:50"},{"boundingBox":"621,123,1017,124,1015,176,619,175","words":[{"boundingBox":"633,122,659,123,657,175,631,174","word":"等"},{"boundingBox":"672,124,697,124,695,176,670,176","word":"待"},{"boundingBox":"710,123,723,124,721,176,708,175","word":"发"},{"boundingBox":"736,124,761,124,759,176,734,176","word":"放"},{"boundingBox":"774,124,800,124,798,176,772,176","word":"奖"},{"boundingBox":"813,124,825,124,823,176,811,176","word":"励"},{"boundingBox":"838,124,928,124,926,176,836,176","word":"0"},{"boundingBox":"941,124,966,124,964,176,939,176","word":"市"},{"boundingBox":"979,124,1017,124,1015,176,977,176","word":"市"}],"text":"等待发放奖励0市市"}]”;


    int tempLowTimes=-1; for(int k=0;k< parameterArray.length();k++) { JSONObject singlexx= parameterArray.getJSONObject(k); String dltTxt=singlexx.getString("text").replace(" ", ""); if (dltTxt.contains("等待发")) { parameterArray.remove(k); k=tempLowTimes; continue; } if (dltTxt.contains("月")&&dltTxt.contains("日")) { parameterArray.remove(k); k=tempLowTimes; continue; } tempLowTimes=tempLowTimes+1; }

                

    本人做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

     

  • 相关阅读:
    【大厂面试06期】谈一谈你对Redis持久化的理解?
    【大厂面试05期】说一说你对MySQL中锁的了解?
    【大厂面试04期】讲讲一条MySQL更新语句是怎么执行的?
    【大厂面试03期】MySQL是怎么解决幻读问题的?
    【大厂面试02期】Redis过期key是怎么样清理的?
    【大厂面试01期】高并发场景下,如何保证缓存与数据库一致性?
    透过面试题掌握Redis【持续更新中】
    MySQL慢查询优化(线上案例调优)
    分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架
    微服务框架Demo.MicroServer运行手册
  • 原文地址:https://www.cnblogs.com/xiaoliao/p/10415214.html
Copyright © 2011-2022 走看看