java - 通过迭代将 object[] 转换为实体类型
问题描述
我有一个类型的 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 ()
解决方案
如果对象数组的顺序是固定的,那么您可以遍历数组并使用相应的索引。
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;
}
推荐阅读
- flutter - 在 Flutter 中向上推 Column 的子项
- android - 我的 android studio 项目(kotlin)中缺少功能
- javascript - 在由 transitionEnd 事件触发时,requestAnimationCallback 后更改元素显示和设置转换不起作用
- python - 标签 Tkinter 问题
- java - Java 字节码错误:期望在堆栈上找到对象/数组
- bash - 如何循环包含名称列表的 file.txt?
- image - 如何避免 UNet 算法偏向于背景,而不是罕见的异常?
- python - Dockerfile 和诗歌 | 执行器运行失败 [/bin/sh -c 诗歌安装 --no-dev]:退出代码:1
- android - Android 内容无法正确显示
- flutter - 带有 Flutter Test 的 Finder 是否始终按照渲染时间的顺序返回小部件?