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]

> 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.
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.
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);
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);
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);
}
}