简体   繁体   English

如何使用 Eclipse EMF 复制通用列表?

[英]How to copy a generic list with Eclipse EMF?

how can i split an Elist to two Elists without getting the NullPointerException.如何将一个 Elist 拆分为两个 Elist 而不会出现 NullPointerException。 I already tried EcoreUtil.copy() / Collections.copy.我已经尝试过 EcoreUtil.copy() / Collections.copy。 The Problem seems that when declaring the copy target List it needs to be initialized with = null;问题似乎是在声明复制目标列表时需要使用 = null; 进行初始化。 I also tried using an Iterator to copy Elements and tried adding them with .set() .add() all exit with the Exception above.我还尝试使用迭代器来复制元素,并尝试使用 .set() .add() 添加它们,所有这些都以上面的异常退出。 The declaration of the target List seems to be working only with an allocation.目标列表的声明似乎只适用于分配。 While debugging i clearly see that the copied object in the List is not null.在调试时,我清楚地看到列表中复制的对象不为空。

 EList<RtTask> tasks = rtModule.getTasks();
 EList<RtModuleInvocation> invoc0 = null; //target List
        for (RtTask rtTask : tasks) {
            EList<RtModuleInvocation> invocations = rtTask.getModuleInvocations(); //src List

Thanks.谢谢。

Thanks to https://www.programcreek.com/java-api-examples/emf i found out the correct way to initialize my Elist with a constructor that Creates an empty instance with no initial capacity.The data storage will be null.感谢https://www.programcreek.com/java-api-examples/emf我找到了使用构造函数初始化我的 Elist 的正确方法,该构造函数创建一个没有初始容量的空实例。数据存储将为空。 and HOP it works.和 HOP 它的工作原理。

EList<RtModuleInvocation> invoc0 = new BasicEList<>();

If you want a copy of a list, you can also use the ECollections utility:如果您想要一个列表的副本,您还可以使用ECollections实用程序:

ECollections.newBasicEList(Iterable)
Creates a mutable BasicEList containing the given elements.创建一个包含给定元素的可变BasicEList

So to copy the RtModuleInvocation list you can use:因此,要复制RtModuleInvocation列表,您可以使用:

ECollections.newBasicEList(rtTask.getModuleInvocations())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM