我正在尝试编写我的第一个Netlogo扩展,它可以与字符串列表进行比较,希望如此。由于我刚开始编写扩展而且对编程Java不太了解,我希望能提供帮助。
到目前为止我的代码是:
package org.nlogo.extensions.string;
import org.nlogo.api.Argument;
import org.nlogo.api.Context;
import org.nlogo.api.DefaultReporter;
import org.nlogo.api.ExtensionException;
import org.nlogo.api.LogoException;
import org.nlogo.api.Syntax;
import org.nlogo.api.LogoListBuilder;
import org.nlogo.api.LogoList;
public class Matching2 extends DefaultReporter{
@Override
public Syntax getSyntax(){
return Syntax.reporterSyntax(new int[] { Syntax.ListType(), Syntax.ListType() },
Syntax.ListType());
}
@Override
public Object report(Argument args[], Context context)
throws ExtensionException, LogoException
{
LogoList input1 = args[0].getList() ;
LogoList input2 = args[1].getList();
return input1.retainAll(input2);
}
}
但是,当我尝试运行模型时,我只会收到错误...
提前谢谢。编辑:
error (UnsupportedOperationException)
while observer running SHOW
called by procedure CHECK-POTENTIAL-WORDS
called by procedure GO
called by Button 'go one time step'
NetLogo is unable to supply you with more details about this error.Please report the problem at https://github.com/NetLogo/NetLogo/issues, or to bugs@ccl.northwestern.edu, and paste the contents of this window into your report.
java.lang.UnsupportedOperationException at org.nlogo.api.LogoList.org$nlogo$api$LogoList$$unsupported(LogoList.scala:67)
at org.nlogo.api.LogoList$Iterator.remove(LogoList.scala:64)
at org.nlogo.api.LogoList$Iterator.remove(LogoList.scala:54)
at java.util.AbstractCollection.retainAll(AbstractCollection.java:410)
at org.nlogo.extensions.string.Matching2.report(Matching2.java:28)
at org.nlogo.prim._externreport.report(_externreport.java:50)
at org.nlogo.prim.etc._show.perform(_show.scala:13)
at org.nlogo.nvm.Context.stepConcurrent(Context.java:91)
at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82)
at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143)
at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at scala.util.control.Exception$Catch.apply(Exception.scala:88)
at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41)
at org.nlogo.job.JobThread.run(JobThread.scala:75)
答案 0 :(得分:0)
LogoList
接口的List
实现不支持retainAll()
方法 - 也就是说,因为AbstractCollection
中该方法的默认实现使用了迭代器remove()
的{{1}}方法unsupported
- 请在此处查看其源代码:https://github.com/NetLogo/NetLogo/blob/c56cffd5b12cf135efc4c3640b3cc0d5dc2acc3c/parser-core/src/main/core/LogoList.scala
解决方法:使用支持此操作的实现创建自己的列表对象(例如LogoList
):
ArrayList
但如果您需要退回List input1 = new ArrayList(args[0].getList());
List input2 = new ArrayList(args[1].getList());
input1.retainAll(input2);
return input1;
,这可能无效 - 请尝试此操作:
LogoList