添加的ArrayList在Java ArrayList的另一 [英] Add ArrayList to another ArrayList in java
问题描述
我有以下Java code,其中我试图到ArrayList复制到另一个的ArrayList。
的ArrayList<串GT;节点=新的ArrayList<串GT;();
ArrayList的节点列表=新的ArrayList();
ArrayList的列表=新的ArrayList(); 的for(int i = 0; I< PropertyNode.getLength() - 1;我++){
节点childNode = PropertyNode.item(ⅰ);
节点列表儿童= childNode.getChildNodes(); 如果(孩子们!= NULL){
nodes.clear();
nodes.add(PropertyStart);
nodes.add(Children.item(3).getTextContent());
nodes.add(Children.item(7).getTextContent());
nodes.add(Children.item(9).getTextContent());
nodes.add(Children.item(11).getTextContent());
nodes.add(Children.item(13).getTextContent());
nodes.add(PropertyEnd); }
NodeList.addAll(节点);
list.add(节点列表);
}
我要的名单数组是格式为:
<$p$p><$c$c>[[PropertyStart,a,b,c,PropertyEnd],[PropertyStart,d,e,f,PropertyEnd],[PropertyStart,......]]不过从上面的code,在名单阵列输出见过这样的:
<$p$p><$c$c>[PropertyStart,a,b,c,PropertyEnd,PropertyStart,d,e,f,PropertyEnd,PropertyStart,....PropertyEnd]我想你可能已经注意到的差异。我不能够达到的效果预期格式。
请给我建议的任何解决方案!
在此先感谢!
然后,你需要的ArrayList
的的ArrayList
的ArrayList&LT; ArrayList的&LT;串GT;&GT;节点=新的ArrayList&LT; ArrayList的&LT;串GT;&GT;();
ArrayList的&LT;串GT;节点列表=新的ArrayList&LT;串GT;();
nodes.add(节点列表);
注意节点列表
已更改为节点列表
。在 Java命名约定变量以小写。开课以大写字母。
I am having the following java code, in which I am trying to copy the ArrayList to another ArrayList.
ArrayList<String> nodes = new ArrayList<String>();
ArrayList NodeList=new ArrayList();
ArrayList list=new ArrayList();
for(int i=0;i<PropertyNode.getLength()-1;i++){
Node childNode = PropertyNode.item(i);
NodeList Children = childNode.getChildNodes();
if(Children!=null){
nodes.clear();
nodes.add("PropertyStart");
nodes.add(Children.item(3).getTextContent());
nodes.add(Children.item(7).getTextContent());
nodes.add(Children.item(9).getTextContent());
nodes.add(Children.item(11).getTextContent());
nodes.add(Children.item(13).getTextContent());
nodes.add("PropertyEnd");
}
NodeList.addAll(nodes);
list.add(NodeList);
}
I want the "list" array to be in this format:
[[PropertyStart,a,b,c,PropertyEnd],[PropertyStart,d,e,f,PropertyEnd],[PropertyStart,......]]
But from the above code, the "list" array output is seen like this:
[PropertyStart,a,b,c,PropertyEnd,PropertyStart,d,e,f,PropertyEnd,PropertyStart,....PropertyEnd]
I think you might have noticed the difference. I am not able to achieve the result in expected format. Please suggest me any solution!! Thanks in advance!!
Then you need a ArrayList
of ArrayLists
:
ArrayList<ArrayList<String>> nodes = new ArrayList<ArrayList<String>>();
ArrayList<String> nodeList = new ArrayList<String>();
nodes.add(nodeList);
Note that NodeList
has been changed to nodeList
. In Java Naming Conventions variables start with a lower case. Classes start with an upper case.
这篇关于添加的ArrayList在Java ArrayList的另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!