只是一个简单的问题。 如何使用包含对象和使用foreach的其他arraylist填充arraylist?
谢谢你 此致答案 0 :(得分:4)
第一个/更好的选择:
ava.lang.NullPointerException: Cannot get property 'result' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:156)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
at Script1.run(Script1.groovy:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:166)
at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:362)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:723)
at hudson.model.Build$BuildExecution.post2(Build.java:185)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:668)
at hudson.model.Run.execute(Run.java:1763)
at hudson.model.FreeStyleBuild.run``(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
使用forEach(Java 8):
targetArrayList.addAll(sourceArrayList);
使用for循环:
sourceArrayList.forEach(element -> targetArrayList.add(element));
答案 1 :(得分:0)
假设您有ArrayList<myObject> list1
,ArrayList<myObject> list2
for(myObject obj : list1){
list2.add(obj);
}
更好地回答使用addAll() list2.addAll(list1)
答案 2 :(得分:0)
您可以使用addAll()收藏集。
ArrayList<Object> list1 = new ArrayList<Object>();
ArrayList<Object> list2 = new ArrayList<Object>();
list2.addAll(list1);
答案 3 :(得分:0)
考虑像这样的对象类型的数据
class UserData{
String user;
String pass;
}
然后将其放入[ArrayList] [1]并从外部或任何内部源填充数据。
ArrayList <UserData> newInfoList = new ArrayList <UserData>()
for (UserData d:??? ) //??? Means your current arraylist
{
newInfoList.add(d);
}