Java列表自动创建并添加

发布于 2025-01-29 09:10:44 字数 389 浏览 2 评论 0原文

是否可以替换它:

    if (archiveUnitEntity.getArchiveCaseEntityList() == null) {
      archiveUnitEntity.setArchiveCaseEntityList(new ArrayList<ArchiveCaseEntity>());
    }
    
    archiveUnitEntity.getArchiveCaseEntityList().add(archiveCaseEntity);

这样的东西:

iflistNullCreate(ArchiveUnitentity.getArchiveCaseentityList())。add(archiveCaseentity);

Is it possible to replace this:

    if (archiveUnitEntity.getArchiveCaseEntityList() == null) {
      archiveUnitEntity.setArchiveCaseEntityList(new ArrayList<ArchiveCaseEntity>());
    }
    
    archiveUnitEntity.getArchiveCaseEntityList().add(archiveCaseEntity);

This something like that:

ifListNullCreate(archiveUnitEntity.getArchiveCaseEntityList()).Add(archiveCaseEntity);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2025-02-05 09:10:44

如果您可以更改方法getArchiveCaseentityList(),则可以将NULL检查中添加到Getter中并在此处更新(或在某些情况下需要为NULL时创建其他方法)。如果不是,那么您可以在任何需要/需要的地方创建所需的方法。

但是我认为没有一种内置方法可以做到这一点。

If you can change the method getArchiveCaseEntityList() then you could add that null check into the getter and update it there (or create another method if you need it to be null in some cases). If not, then you could create the method you want wherever you want/need.

But I don't think there is a built-in method to do that.

勿挽旧人 2025-02-05 09:10:44

这是一个可能的解决方案。

ArchiveUnitentity类中,声明这样的列表,以免创建列表。

private List<ArchiveCaseEntity> archiveCaseEntityList = new ArrayList<>();

您可能还想更改该字段的设置器,以便无法设置为空。

public void setArchiveCaseEntityList(ArchiveCaseEntityList archiveCaseEntityList) {
    this.archiveCaseEntityList = archiveCaseEntityList == null ? new ArrayList<ArchiveCaseEntity>() : archiveCaseEntityList;
}

Here's one possible solution.

In the ArchiveUnitEntity class, declare the list like this, so that it's not null on creation.

private List<ArchiveCaseEntity> archiveCaseEntityList = new ArrayList<>();

You might also want to change the setter for that field, so that it can't get set to null.

public void setArchiveCaseEntityList(ArchiveCaseEntityList archiveCaseEntityList) {
    this.archiveCaseEntityList = archiveCaseEntityList == null ? new ArrayList<ArchiveCaseEntity>() : archiveCaseEntityList;
}
红颜悴 2025-02-05 09:10:44

最好在创建存档实例的过程中初始化您的ArchiveCaseEntityList属性。避免零对象可以简化很多事情:)

当您声明自己的属性时,直接直接:

private List<ArchiveCaseEntity> archiveCaseEntityList = new ArrayList<>();

在构造函数中:

public ArchiveUnitEntity(){
   this.archiveCaseEntityList = new ArrayList<>();
}

最后,您可以构建一种新方法来管理ArchIveCaseentity添加:

public void addArchiveCaseEntity(ArchiveCaseEntity entity){
   this.archiveCaseEntityList.add(entity);
}

It's better to initialize your archiveCaseEntityList attribute during the creation of the instance of archiveUnitEntity. Avoiding null object can simplify a lot of things :)

Directly when you declare your attribute :

private List<ArchiveCaseEntity> archiveCaseEntityList = new ArrayList<>();

or in constructor :

public ArchiveUnitEntity(){
   this.archiveCaseEntityList = new ArrayList<>();
}

And finally, you could build a new method to manage ArchiveCaseEntity addition :

public void addArchiveCaseEntity(ArchiveCaseEntity entity){
   this.archiveCaseEntityList.add(entity);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文