本文介绍了如何Concat的2的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个的ArrayList大小相同。 ArrayList的1由10名和ArrayList 2由他们的电话号码的。我想的名字和号码Concat的到一个ArrayList中。我该怎么做呢?
I have 2 arraylists of equal size. Arraylist 1 consists of 10 names and arraylist 2 consists of their phone numbers. I want to concat the names and number into one arraylist. How do i do this ?
推荐答案
您可以使用以第二列表的元素添加到第一个:
You can use .addAll()
to add the elements of the second list to the first:
array1.addAll(array2);
编辑:根据您的澄清以上(我想有两个名称和编号的新的ArrayList一个String 的),你会想循环通过第一个列表,添加从第二个名单给它的项目。
Based on your clarification above ("i want a single String in the new Arraylist which has both name and number."), you would want to loop through the first list and append the item from the second list to it.
事情是这样的:
int length = array1.size();
if (length != array2.size()) { // Too many names, or too many numbers
// Fail
}
ArrayList<String> array3 = new ArrayList<String>(length); // Make a new list
for (int i = 0; i < length; i++) { // Loop through every name/phone number combo
array3.add(array1.get(i) + " " + array2.get(i)); // Concat the two, and add it
}
如果你把:
array1 : ["a", "b", "c"]
array2 : ["1", "2", "3"]
您将获得:
array3 : ["a 1", "b 2", "c 3"]
这篇关于如何Concat的2的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!