我想要n个数字,这表示我的ArrayList的最后一条记录,但是问题是我的ArrayList是自定义POJO类,它包含自己的数组,这就是为什么我无法识别ArrayList包含多少记录的原因
请检查我要从其创建POJO类的JSON
{
"data": [
{
"id": "7",
"name": "Grand Father Name",
"parent_id": "0",
"relation_type": "grand-father",
"children": [
]
},
{
"id": "8",
"name": "Grand Mother Name",
"parent_id": "0",
"relation_type": "grand-mother",
"children": [
{
"id": "9",
"name": "Father Name",
"parent_id": "8",
"relation_type": "father",
"children": [
{
"id": "11",
"name": "My Name",
"parent_id": "9",
"relation_type": "self",
"children": [
]
}
]
},
{
"id": "10",
"name": "Mother Name",
"parent_id": "8",
"relation_type": "mother",
"children": [
]
}
]
}
]
}
POJO类
public class Tree {
@SerializedName("data")
@Expose
public ArrayList<Child> data = null;
public class Child {
@SerializedName("family_id")
@Expose
public int family_id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("relation_type")
@Expose
public String relation_type;
@SerializedName("parent_id")
@Expose
public int parentId;
@SerializedName("children")
@Expose
public List<Child> children = null;
public int getId() {
return family_id;
}
public void setId(int id) {
this.family_id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
}
}
如您所见,Child类包含其自己的列表,所以我无法确定该列表将包含多少条记录
希望可以解决我的问题,我们将不胜感激。 预先感谢
答案 0 :(得分:1)
您可以递归执行此操作:
ArrayList<Child> list = new ArrayList<>(); //this is the list where you want to fill all children
void fillRecursively(ArrayList<Child> root){
List<Child> children = root.getChildren();
for(Child child : children){
if(child.getChildren().size()!=0){
fillRecursively(child);
}else{
list.add(child);
}
}
}
}
也不要忘记将根节点添加到列表中。
list.add(root); //at the beginning