Java JSONArray for循環 remove成員的一個好算法

 很久沒寫日誌了,今天來寫一個,JSONArry fro循環判斷value,移除不須要的,不需再將JSONArry再次轉換成 map或list。android

 

JSONArray移除成員只能經過for遍歷取成員,判斷後remove(index),這樣很明顯是不行的,除非移除的成員只有1個,不然再你移除後for循環的遍歷次數就被打亂了。你就會移除到不須要移除的成員。處理這個問題的方式其實不少的,好比建立list範型map將json遍歷後一個一個弄到他們裏面去......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市市"}]」;spa


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多萬,各類當前熱門的網絡手機獎勵紅包所有集成,另外還有熱門電影和淘寶高額優惠券!很適合各種型的用戶。日誌

 

相關文章
相關標籤/搜索