如何动态创建Java类List?
我正在使用 groovy 1.8,我想要一个类似这样的列表List
这样,使用下面的循环我可以向此列表添加更多内容,我该怎么做?
def queryxml=new XmlSlurper().parse(QueryProvider.class.getResourceAsStream( '/queries.xml' ))
queryxml.query.each { node ->
operation="${[email protected]()}"
if(operation.equals(op.create.toString()))
{
query="${node.text()}"
println "$query"
MyQueryClass myQuery=new MyQueryClass (query)
queryl.add(myQuery)
}
}
I am using groovy 1.8 where i want to have a list something like thisList<MyQueryClass> queryl=new List<MyQueryClass>()
So that using the below loop i can add more to this list, how can I do that?
def queryxml=new XmlSlurper().parse(QueryProvider.class.getResourceAsStream( '/queries.xml' ))
queryxml.query.each { node ->
operation="${[email protected]()}"
if(operation.equals(op.create.toString()))
{
query="${node.text()}"
println "$query"
MyQueryClass myQuery=new MyQueryClass (query)
queryl.add(myQuery)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,我无法正确表达我的问题。
我想通了,因为我无法从抽象接口 java.util.List 中创建实例
来完成这项工作。谢谢蒂姆和德拉甘斯坦科维奇
My apologies, I could not convey my question properly.
I figured it out, as I cannot create an instance out of a abstract interface java.util.List
does the work. Thanks tim and draganstankovic
您没有给我们太多信息,但假设您有一个类:
并且您的 XML 类似于:
那么您应该能够使用
findAll
将 xml 限制为仅感兴趣的节点,然后使用collect
来构建对象列表,如下所示:然后打印出:
PS: The
groovy.transform.Canonical
注释 此处解释You don't give us much to go on, but assuming you have a class:
And your XML is similar to:
Then you should be able to use
findAll
to limit the xml to just the nodes of interest, followed bycollect
to build up your list of objects like so:And that prints out:
PS: The
groovy.transform.Canonical
annotation is explained here