Java列表自动创建并添加
是否可以替换它:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以更改方法
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.
这是一个可能的解决方案。
在
ArchiveUnitentity
类中,声明这样的列表,以免创建列表。您可能还想更改该字段的设置器,以便无法设置为空。
Here's one possible solution.
In the
ArchiveUnitEntity
class, declare the list like this, so that it's not null on creation.You might also want to change the setter for that field, so that it can't get set to null.
最好在创建存档实例的过程中初始化您的ArchiveCaseEntityList属性。避免零对象可以简化很多事情:)
当您声明自己的属性时,直接直接:
在构造函数中:
最后,您可以构建一种新方法来管理ArchIveCaseentity添加:
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 :
or in constructor :
And finally, you could build a new method to manage ArchiveCaseEntity addition :