Java 将一个 ArrayList 的元素添加到另一个 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29443062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add elements of one ArrayList to another ArrayList
提问by koshish kharel
Is it possible to add the elements of one Arraylist to another Arraylist? For example if an Arraylist has elements 3,6,3,8,5 in index 0,1,2,3,4, now I want to add 3,6,3,8,5 to another ArrayList in index 0, is it possible?
是否可以将一个 Arraylist 的元素添加到另一个 Arraylist?例如,如果一个 Arraylist 在索引 0、1、2、3、4 中有元素 3、6、3、8、5,现在我想将 3、6、3、8、5 添加到索引 0 中的另一个 ArrayList,是有可能吗?
ArrayList<String> num = new ArrayList<String>();
num.add("3");
num.add("6");
num.add("3");
num.add("8");
num.add("5");
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < num.size(); i++)
{
result.addAll(i,num);
}
I have tried this but it is not working.
what i want is when i try System.out.println(result.get(0));
result must be [3 6 3 8 5].
我试过这个,但它不起作用。我想要的是当我尝试System.out.println(result.get(0));
结果必须是 [3 6 3 8 5]。
回答by malli
Try this sample code
试试这个示例代码
ArrayList<ArrayList<String>> nodes = new ArrayList<ArrayList<String>>();
ArrayList<String> nodeList = new ArrayList<String>();
nodes.add(nodeList);
回答by jeremie
Your result list needs to be nested. It should have this kind of form:
您的结果列表需要嵌套。它应该有这种形式:
List<List<Integer>> result = new ArrayList<>();
You can then just do that
然后你可以这样做
result.add(0, num);
回答by silentprogrammer
To simply copy all elements you can do
简单地复制你可以做的所有元素
ArrayList<String> result = new ArrayList<String>(num);
and if you want to copy all the elements at a particular index you have to change the result ArrayList
如果要复制特定索引处的所有元素,则必须更改结果 ArrayList
ArrayList<List<String>> result = new ArrayList<List<String>>();
result.add(0, num); // 0 is the index
回答by Stephen C
I think what you are trying to do is this:
我认为您正在尝试做的是:
for (int i = 0; i < num.size(); i++) {
result.add(i, num.get(i));
}
Or maybe just this:
或者也许只是这个:
result.addAll(num);
What your current code does is you add all of num
to result
many times ... at successive starting positions. That is ... strange.
你目前的代码不会是你所有的添加num
到result
多次......在连续的首发位置。那是……奇怪。
UPDATE
更新
What i want is when i try
System.out.println(result.get(0));
result must be[3 6 3 8 5]
.
我想要的是当我尝试
System.out.println(result.get(0));
结果必须是[3 6 3 8 5]
.
Ah ... I get it ... you are trying to create a list of strings where the strings are representations of the input lists:
啊......我明白了......你正在尝试创建一个字符串列表,其中字符串是输入列表的表示:
Do this:
做这个:
for (int i = 0; i < num.size(); i++) {
result.add(i, num.toString());
}
This will give you the output you are asking for.
这将为您提供所需的输出。
Another possibility is that you want a list of lists of strings:
另一种可能性是您想要一个字符串列表列表:
ArrayList<ArrayList<String>> result = new ArrayList<>();
for (int i = 0; i < num.size(); i++) {
result.add(i, num);
}
That will alsogive you the output you are asking for ... though for a different reason.
这也将为您提供您要求的输出......尽管原因不同。
回答by Saurabh Jhunjhunwala
do not use all method if you are putting it inside a loop, you should use add
如果你把它放在一个循环中,不要使用 all 方法,你应该使用 add
if at all you want to use addAll put it outside the loop.
如果你想使用 addAll 把它放在循环之外。
回答by Anusha B
ArrayList<String> obj1 = new ArrayList<String>();
obj1.add("3");
obj1.add("6");
obj1.add("3");
obj1.add("8");
obj1.add("5");
ArrayList obj2 = new ArrayList();
obj2.add(0, obj1);;
System.out.println("Size of the arraylist is --> "+obj2.size());
System.out.println("Value at index 0 --> "+obj2);
回答by user9562951
ArrayList<String> num = new ArrayList<String>();
num.add("3");
num.add("6");
num.add("3");
num.add("8");
num.add("5");
ArrayList<String> result = new ArrayList<String>();
result.addAll(num);