繁体   English   中英

在arraylist的arraylist中动态添加arraylist

[英]Dynamically add arraylist in arraylist in arraylist

我试图在arraylist的arraylist中添加一个arraylist,但是在连续进行第一个循环之后,添加下一个插入的第一个数组。

 List<ArrayList<List<String>>> tlist= new ArrayList<>();
 List<List<String>> tslist= new ArrayList<>();
 List<String> childcat= new ArrayList<>();
 for (int i = 0; i < response.length(); i++) {
     JSONObject person = (JSONObject) response.get(i);
     for (int j = 0; j < sub.length(); j++) {
         JSONObject subobj = sub.optJSONObject(j);
         JSONArray suba = subobj.optJSONArray("SubMenus");
         childcat = new ArrayList<>();
         if(suba!=null) {
             for (int k = 0; k < suba.length(); k++) {
                 JSONObject subobja = suba.optJSONObject(k);
                 childcat.add(subtitlea);
             }
         }
         tslist.add(new ArrayList<String>(childcat));
     }
     tlist.add((new ArrayList<>(tslist)));  
 }

@Maroun Maroun对问题的评论。

如果您坚持使用此代码;

将其添加到列表后,创建一个新对象(Arraylist)。 不需要在第一个循环之前创建所有对象。 尝试如下

     List<ArrayList<List<String>>> tlist= new ArrayList<>();
     for (int i = 0; i < response.length(); i++) {
         JSONObject person = (JSONObject) response.get(i);
         List<List<String>> tslist= new ArrayList<>();
         for (int j = 0; j < sub.length(); j++) {
             JSONObject subobj = sub.optJSONObject(j);
             JSONArray suba = subobj.optJSONArray("SubMenus");
             List<String> childcat= new ArrayList<>();
             if(suba!=null) {
                 for (int k = 0; k < suba.length(); k++) {
                     JSONObject subobja = suba.optJSONObject(k);
                     childcat.add(subtitlea);
                 }
             }
             tslist.add(new ArrayList<String>(childcat));
         }
         tlist.add((new ArrayList<>(tslist)));  
     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM