如何连接两个arraylist <string>并存储到第三个arraylist <string>?</string> </string>

时间:2012-06-18 09:21:44

标签: java arraylist

我有三个arraylist,在这里我必须连接2个arraylist并将其显示为第三个arraylist。我的编码如下:

编码::

List<String> firstName  = new ArrayList<String>();
List<String> lastName  = new ArrayList<String>();
         /////inserting first name
         String string1 = "aaa";
         firstName.add(string1);
         String string2 = "bbb";
         firstName.add(string2);
         ///////inserting last name
         String string3 = "yyy";
         lastName.add(string3);
         String string4 = "zzz";
         lastName.add(string4);

 /////////first name 
        Iterator<String> iterator = firstName.iterator();
        while(iterator.hasNext()){
         String FirstName = iterator.next();
         System.out.println(" FirstName   "+FirstName);
        }
 //////////////last name
        Iterator<String> iterator1 = lastName.iterator();
        while(iterator1.hasNext()){
         String LastName = iterator1.next();
         System.out.println(" LastName  "+LastName);
        }
List<String> name=new ArrayList<String>();
        ///////////name=firstname+lastname
        Iterator<String> iterator11 = lastName.iterator();
        while(iterator11.hasNext()){
         name=firstName+lastName;///////here getting error
               ////////////what should I do here to add two arraylist<String> value
         System.out.println(" Name = FirstName+LastName  "+name);
        }

我想要的输出::

FirstName   aaa
FirstName   bbb
LastName  yyy
LastName  zzz
Name = FirstName+LastName aaayyy
Name = FirstName+LastName bbbzzz

但是我不知道如何连接两个arrayList并在第三个arrayList中显示它,请帮帮我..先谢谢..

6 个答案:

答案 0 :(得分:7)

如果列表大小相等,您可以将它们一起迭代:

Iterator<String> iterator = firstName.iterator();
Iterator<String> iterator1 = lastName.iterator();

while(iterator.hasNext() && iterator1.hasNext()){
    String FirstName = iterator.next();
    String LastName = iterator1.next();
    // add FirstName and LastName to the new list here
}

<强>更新

Iterator<String> iterator = firstName.iterator();
Iterator<String> iterator1 = lastName.iterator();
List<String> newList = new ArrayList<String>();

while(iterator.hasNext() && iterator1.hasNext()){
    String FirstName = iterator.next();
    String LastName = iterator1.next();
    newList.add(FirstName + " " + LastName);
}

答案 1 :(得分:4)

List<String> newList = new ArrayList<String>(firstName);
newList.addAll(lastName);

newList将包含firstName和LastName

中的元素

编辑:以上代码连接两个列表,在名字列表的末尾添加lastname。如果列表的长度相同,那么您可以尝试:

List<String> newList = new ArrayList<String>();
for(int i=0;i<fistName.length();i++)
{
    newList.Add(firstName.get(i)+lastName.get(i));
}

Iterator<String> iter = newList.iterator();
while(iter.hasNext()){
    System.out.println(" Name = FirstName+LastName  "+iter.next());
    }

这将为您提供所需的列表。

答案 2 :(得分:1)

你应该循环。

List<String> name=new ArrayList<String>();
for(i=0;i<firstname.length()&&i<lastname.length();i++){
    name.add(firstname.get(i) + lastname.get(i));
}

答案 3 :(得分:0)

所以你真的想要创建一个由两个其他列表的元素的成对串联形成的字符串列表。

最简单的方法就是写一个循环;

 ArrayList<String> l1 = ...
 ArrayList<String> l2 = ...
 ArrayList<String> l3 = new ArrayList<String>(l1.size());
 for (int i =- 0; i < l3.size(); i++) {
     l3.add(l1.get(i) + l2.get(i));
 }

这假定l1l2具有相同的长度。 (注意:我故意在声明中使用list实现类,因为这个代码只适用于ArrayList。对于LinkedList,你需要使用list迭代器,因为{ {1}}很贵。)

答案 4 :(得分:0)

public static void main(String[] args) {

    List<String> firstName = new ArrayList<String>();
    List<String> lastName = new ArrayList<String>();
    String string1 = "aaa";
    firstName.add(string1);
    String string2 = "bbb";
    firstName.add(string2);
    // /////inserting last name
    String string3 = "yyy";
    lastName.add(string3);
    String string4 = "zzz";
    lastName.add(string4);

    Iterator<String> iterator = firstName.iterator();
    Iterator<String> iterator1 = lastName.iterator();
    List<String> name = new ArrayList<String>();


    while (iterator.hasNext()&& iterator1.hasNext() ) {

        name.add(iterator.next()+" "+iterator1.next());
    }

    Iterator<String> iterator11 = name.iterator();
    while(iterator11.hasNext())
        System.out.println(iterator11.next());

}

答案 5 :(得分:0)

ArrayList<String> fname=new ArrayList<String>();
fname.add("deepak");
fname.add("avinash");
fname.add("amit");
ArrayList<String> lname=new ArrayList<String>();
lname.add("goswami");
lname.add("singh");
lname.add("jha");
ArrayList<String> name=new ArrayList<String>();
Iterator<String> it1=fname.iterator();
Iterator<String> it2=lname.iterator();
while(it1.hasNext() && it2.hasNext()){
    String f=it1.next();
    String l=it2.next();
    name.add(f+" "+l);
}
for(String str:name){
    System.out.println(str);
}

} }

输出:

deepak goswami avinash singh amit jha