I am trying to add an arraylist in an arraylist in an arraylist but after the first loop in continuous to add the first array insted of the next ones..
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)));
}
Listen @Maroun Maroun as commented on question.
If you insist on this code;
Create a new object(Arraylist) after adding it to list. It is not required create all objects before first loop. Give it a try as below
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)));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.