Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从包含自身数组的对象数组中获取n条记录_Java_Android_Json_Pojo - Fatal编程技术网

Java 如何从包含自身数组的对象数组中获取n条记录

Java 如何从包含自身数组的对象数组中获取n条记录,java,android,json,pojo,Java,Android,Json,Pojo,我想要n个数字,这意味着我的ArrayList的最后一条记录,但问题是我的ArrayList是自定义POJO类,它包含自己的数组,这就是为什么我无法确定ArrayList包含多少条记录的原因 请检查下面我创建POJO类的JSON { "data": [ { "id": "7", "name": "Grand Father Name", "parent_id": "0", "relation_type": "grand-father",

我想要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": [

          ]
        }
      ]
    }
  ]
}
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;
        }
    }
}
波乔班

{
  "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": [

          ]
        }
      ]
    }
  ]
}
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;
        }
    }
}
公共类树{
@SerializedName(“数据”)
@暴露
公共ArrayList数据=null;
公营儿童{
@SerializedName(“家族id”)
@暴露
公共int家庭id;
@序列化名称(“名称”)
@暴露
公共字符串名称;
@SerializedName(“关系类型”)
@暴露
公共字符串关系类型;
@SerializedName(“父项id”)
@暴露
公共int-parentId;
@序列化名称(“子项”)
@暴露
public List children=null;
公共int getId(){
返回家庭标识;
}
公共无效集合id(内部id){
this.family_id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public int getParentId(){
返回parentId;
}
public void setParentId(int parentId){
this.parentId=parentId;
}
公共列表getChildren(){
返回儿童;
}
公共子项(列出子项){
这个。孩子=孩子;
}
}
}
正如您所看到的,子类包含自己的列表,因此我无法确定此列表将包含多少记录

希望它能解决我的问题,任何帮助都将被感激。
提前感谢

您可以递归执行此操作:

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);
   }
 }
}
}

您的意思是要计算包括子数组在内的数组元素数吗?所以你想递归计数,对吗?是的,实际上它的子元素再次包含它的子元素数组,所以我想得到所有这些记录,直到最后一个记录