通过迭代将object []转换为实体类型

时间:2019-06-26 11:15:41

标签: java

我有一个类型的

pojo
 public class Comain implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Integer ComainId;

    private String name;

    private String Cescription;


     private boolean  isAvailable ;
     }

下面是我提取结果的代码

   List<Object[]>  d = new ArrayList<Object[]>();
   d = ARepository.getAb();

在调试时,我分析得出以下格式的结果

 d = {ArrayList}
    > o = {object[4]}
       > o = {Integer}1
       > 1 = "qqq"
       > 2 = "ddddd"
       > 3 = {Boolean} false
     > 1 = {object[4]}
       > o = {Integer}2
       > 1 = "qrtq"
       > 2 = "rrddd"
       > 3 = {Boolean} true

现在我想将object []数组转换为Comain pojo的pojo类型,现在,尽管我已经创建了对象类型Comain,请告知如何实现相同的目标

   Comain C = new Comain ()

debug image

1 个答案:

答案 0 :(得分:2)

如果对象数组的顺序是固定的,则可以遍历数组并使用相应的索引。

List<Comain> comains = new ArrayList<>();
for (Object[] objects : d) {
    comains.add(new Comain((Integer) objects[0], (String) objects[1], (String) objects[2], (Boolean) objects[3]));
}

当然,您必须为Comain类添加一个构造函数。

public Comain(Integer comainId, String name, String cescription, boolean isAvailable) {
    this.ComainId = comainId;
    this.name = name;
    this.Cescription = cescription;
    this.isAvailable = isAvailable;
}