add arraylist to the end of another arraylist

been scouring the forum and cant figure this out.

i want to add all the elements from one arraylist to another arraylist. i dont want to iterate through the list and add them individually. and thought i could use addAll();

when i do arrayList1.addAll(arrayList2) it says incompatible types. how can i make it work or get something similar to work?

thanks

chris

[391 byte] By [ChrisCVa] at [2007-10-2 5:46:16]
# 1
> when i do arrayList1.addAll(arrayList2) it says> incompatible types. First, verify that arrayList2 is a Collection. Second, ensure that if the type of arrayList1 has been specified that all the elements in arrayList2 are of that type.
yawmarka at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 2
You must be trying to smash together two lists of different types. If you're using generics, you can't have a List<String> and add to it a List<Date>, for instance.
targaryena at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 3

we can add arralist to the end of arrayList using addAll().

It is working fine.

Example.

ArrayList al=new ArrayList();

al.add("A");

al.add("B");

al.add("C");

al.add("D");

ArrayList arrayList=new ArrayList();

arrayList.add("E");

arrayList.add("F");

arrayList.add("G");

arrayList.add("H");

arrayList.addAll(al);

System.out.println("arrayList"+arrayList);

muralireddyta at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 4

ArrayList al=new ArrayList();

al.add("A");

al.add("B");

al.add("C");

al.add("D");

ArrayList arrayList=new ArrayList();

arrayList.add("E");

arrayList.add("F");

arrayList.add("G");

arrayList.add("H");

arrayList.addAll(al);

out.println("arrayList"+arrayList);

muralireddyta at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 5
> we can add arralist to the end of arrayList using> addAll().> It is working fine.Perhaps you didn't read the first few posts carefully?
targaryena at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 6

even we can add different types of objects into array list

*/

public class ArrayList_1

{

public static void main(String args[])

{

System.out.println("hi");

ArrayList al=new ArrayList();

al.add(Integer.valueOf("23"));// integer object

al.add("A");// String object

al.add("B");

al.add("C");

al.add("D");

ArrayList arrayList=new ArrayList();

arrayList.add("E");

arrayList.add("F");

arrayList.add("G");

arrayList.add("H");

arrayList.addAll(al);

System.out.println("arrayList"+arrayList);

}

}

Sunil_chda at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...
# 7
> even we can add different types of objects into arrayYou shouldn't though. And if you use generics properly, it won't let you.
targaryena at 2007-7-16 1:56:05 > top of Java-index,Core,Core APIs...