如何在Java中向多维数组添加对象?附JavaScript示例参考
如何在Java中实现向嵌套集合添加对象?
嗨,我明白你习惯了JavaScript里灵活的对象字面量和数组操作,但Java是强类型语言,没法直接像JS那样写匿名对象。我们需要先定义对应的数据实体类,再用List(Java里的动态集合,对应JS的数组)来实现类似的功能。
步骤1:定义实体类
首先,我们要把你JS里的两个层级的对象转换成Java类:
- 内层的
LicenceList元素 → 定义LicenceItem类 - 外层的数组元素 → 定义
LicenceWrapper类
// 对应JS中LicenceList里的单个对象 public class LicenceItem { private String billDurationName; private String planName; private String licenceCreatedDate; private String vehicleId; private String trailperday; // 生成无参构造、全参构造、getter和setter方法(IDE可以自动生成) public LicenceItem() {} public LicenceItem(String billDurationName, String planName, String licenceCreatedDate, String vehicleId, String trailperday) { this.billDurationName = billDurationName; this.planName = planName; this.licenceCreatedDate = licenceCreatedDate; this.vehicleId = vehicleId; this.trailperday = trailperday; } // getter和setter方法 public String getBillDurationName() { return billDurationName; } public void setBillDurationName(String billDurationName) { this.billDurationName = billDurationName; } public String getPlanName() { return planName; } public void setPlanName(String planName) { this.planName = planName; } public String getLicenceCreatedDate() { return licenceCreatedDate; } public void setLicenceCreatedDate(String licenceCreatedDate) { this.licenceCreatedDate = licenceCreatedDate; } public String getVehicleId() { return vehicleId; } public void setVehicleId(String vehicleId) { this.vehicleId = vehicleId; } public String getTrailperday() { return trailperday; } public void setTrailperday(String trailperday) { this.trailperday = trailperday; } } // 对应JS中最外层的数组元素 public class LicenceWrapper { private String plicenceKey; // 注意你JS里的拼写是plicenceKey,这里保持一致 private List<LicenceItem> licenceList; // 构造方法、getter和setter public LicenceWrapper() {} public LicenceWrapper(String plicenceKey, List<LicenceItem> licenceList) { this.plicenceKey = plicenceKey; this.licenceList = licenceList; } public String getPlicenceKey() { return plicenceKey; } public void setPlicenceKey(String plicenceKey) { this.plicenceKey = plicenceKey; } public List<LicenceItem> getLicenceList() { return licenceList; } public void setLicenceList(List<LicenceItem> licenceList) { this.licenceList = licenceList; } }
步骤2:模拟你的JS逻辑,实现添加操作
接下来我们创建集合实例,模拟你JS里的obj数组,然后创建要添加的LicenceItem,最后调用add()方法(对应JS的push())添加到指定的列表中:
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { // 1. 创建内层的LicenceList集合,对应JS里的LicenceList数组 List<LicenceItem> licenceList = new ArrayList<>(); // 添加初始的三个元素 licenceList.add(new LicenceItem("Month(s)", "jhkhjg", "2018fdg01-02 12:14:56", "90", "0")); licenceList.add(new LicenceItem("ffdhfg(s)", "jhkhjg", "fdghdfg-01-12:14:56", "78", "0")); licenceList.add(new LicenceItem("dfhfgh(s)", "jhkhjg", "dfghfg-01-02 12::56", "67", "0")); // 2. 创建外层的集合,对应JS里的obj数组 List<LicenceWrapper> obj = new ArrayList<>(); obj.add(new LicenceWrapper("BI-NBPM39", licenceList)); // 3. 创建要添加的新对象,对应JS里的变量b LicenceItem b = new LicenceItem("test", "ghgfd", "ghf-01-02 12::56", "34", "0"); // 4. 实现和JS中obj[0].LicenceList.push(b)一样的操作 obj.get(0).getLicenceList().add(b); // 验证:打印添加后的元素数量,应该是4个 System.out.println("添加后的LicenceList元素数量:" + obj.get(0).getLicenceList().size()); } }
关键说明
- Java里用
List(这里用ArrayList实现)代替JS的数组,因为数组长度固定,而List支持动态添加元素,add()方法就对应JS的push()。 - 必须定义实体类是因为Java是强类型语言,所有对象都需要明确的类型定义,不像JS可以直接创建匿名对象。
- 如果你用的是Java 16+,可以用**记录类(Record)**简化实体类的定义,比如:
public record LicenceItem(String billDurationName, String planName, String licenceCreatedDate, String vehicleId, String trailperday) {},这样就不用自己写getter和构造方法了。
内容的提问来源于stack exchange,提问作者shunmugarajan
相关产品推荐
相关产品推荐

