0

我有三个数组列表,在这里我必须连接 2 个数组列表并将其显示到第三个数组列表中。我的编码如下所示,

编码::

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中,请帮助我..提前谢谢..

4

6 回答 6

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);
}
于 2012-06-18T09:26:19.487 回答
4
List<String> newList = new ArrayList<String>(firstName);
newList.addAll(lastName);

newList 将包含来自 firstName 和 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());
    }

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

于 2012-06-18T09:23:50.123 回答
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));
}
于 2012-06-18T09:27:30.263 回答
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具有相同的长度。(注意:我故意在此处的声明中使用列表实现类,因为此代码仅适用于使用ArrayList. 对于 aLinkedList您需要使用列表迭代器,因为get(i)它很昂贵。)

于 2012-06-18T09:29:26.207 回答
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());

}
于 2012-06-18T09:39:54.923 回答
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);
}

} }

输出 :

迪帕克·哥斯瓦米·阿维纳什·辛格·阿米特·贾

于 2014-12-19T18:08:44.437 回答