Help With ArrayList

Hi All,

I want to compare elements of two arraylist 's wherein one Arraylist is

Arraylist1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

and the other arraylist is

Arraylist2 = [1, 2, 6, 49, 9]

and then my output should be

output:

Arraylist2 = [1,2,6,9]

i.e. It should just retain the common elements present in the Arraylist1 in Arraylist2

I dont know much about collections ...

I have written a code wherein I have created two arraylists but I dont know how to proceed further,....

MyCode:

import java.util.ArrayList;

publicclass ArrayListExample

{

publicstaticvoid main(String[] args)

{

ArrayList Arraylist1 =new ArrayList();

for (int i = 0; i < 10; i++)

{

parentList.add(i);

}

System.out.println(Arraylist1);

ArrayList Arraylist2 =new ArrayList();

childList.add(1);

childList.add(2);

childList.add(6);

childList.add(49);

childList.add(9);

System.out.println(Arraylist2);

}

}

Will this statement help...

I Could just come up with this..

if(Arraylist1.equals(Arraylist2))

Please help

[1737 byte] By [New_to_Javaa] at [2007-11-27 8:22:09]
# 1
I believe there is a method in the API that can do that for you. But if you have to write the code yourself, then iterate over the list you want to retain, for each item search in the other list, if it doesn't exist delete it.
floundera at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...
# 2
HI,There is a method "contains" in ArrayList.You can use that method to solve your problem.thanksdaniel
java4every1a at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...
# 3

Hi,

This code is answer for your question. As suggested by Daniel you can even make use of ArrayList method contains.

import java.util.ArrayList;

public class ArrayListExample

{

public static void main(String[] args)

{

//Declaration of 3 ArrayList

ArrayList Arraylist1 = new ArrayList();

ArrayList Arraylist2 = new ArrayList();

ArrayList Arraylist3 = new ArrayList();

//Adding elements to Arraylist1

Arraylist1.add("1");

Arraylist1.add("2");

Arraylist1.add("3");

Arraylist1.add("4");

Arraylist1.add("5");

Arraylist1.add("6");

Arraylist1.add("7");

Arraylist1.add("8");

Arraylist1.add("9");

Arraylist1.add("0");

System.out.println("Fist ArrayList "+Arraylist1);

//Adding elements to Arraylist2

Arraylist2.add("1");

Arraylist2.add("2");

Arraylist2.add("6");

Arraylist2.add("49");

Arraylist2.add("9");

System.out.println("Second ArrayList "+Arraylist2);

//Size of ArrayList1

int arrayList1_Length = Arraylist1.size();

//Size of ArrayList2

int arrayList2_Length = Arraylist2.size();

//compare logic

for (int i=0;i<arrayList1_Length;i++)

{

Object arrayList1_obj = Arraylist1.get(i);

for (int j=0;j<arrayList2_Length; j++)

{

Object arrayList2_obj = Arraylist2.get(j);

if (arrayList2_obj.equals(arrayList1_obj))

{

Arraylist3.add(arrayList2_obj);

}

}

}

System.out.println(Arraylist3);

}

}

[code]

[/code]

-Kiran>

kiransonajea at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks a lot..

I tried your code it works ..

I found a another way to do it.. by using retainAll() method.

Thanks a lot..

import java.util.ArrayList;

public class MainArrayListExample

{

public static void main(String[] args)

{

ArrayList parentList = new ArrayList();

for (int i = 0; i < 50; i++)

{

parentList.add(i);

}

System.out.println(parentList);

ArrayList childList = new ArrayList();

childList.add(1);

childList.add(2);

childList.add(25);

childList.add(33);

childList.add(57);

childList.add(49);

childList.add(35);

System.out.println(childList);

ArrayList listInBoth = new ArrayList(parentList);

listInBoth.retainAll(childList);

System.out.println(listInBoth);

}

}

New_to_Javaa at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...
# 5
> I found a another way to do it.. by using retainAll() method.That it! I couldn't remember and couldn't be assed looking it up especially since it is your work not mine.
floundera at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...
# 6
Sorry, But I did not get you..
New_to_Javaa at 2007-7-12 20:10:47 > top of Java-index,Java Essentials,New To Java...