Home > DeveloperSection > Forums > how to compare two ArrayLists of type ArrayList<String[]>?
Royce Roy
Royce Roy

Total Post:134

Posted on    April-22-2015 1:32 AM

 Java Java  Array List 

 1 Reply(s)
 547  View(s)
Rate this:
I am fairly new to the concept of arraylists. I have some data in two arraylist of type String[]. I want to compare them and store them in a new ArrayList<String[]>. 
following is my code:

ArrayList<String[]> org = arr2;
ArrayList<String[]> text = arr3;
ArrayList<String[]> outList = new ArrayList<>() ;
for(int i = 0; i < org.size(); i++) {
    for(int x = 0; x < org.get(i).length; x++) {
        System.out.printf("OrgId[%d][%d]: ", i, x);
for(int i = 0; i < text.size(); i++) {
    for(int x = 0; x < text.get(i).length; x++) {
        System.out.printf("file[%d][%d]: ", i, x);

the values for org and text are as follows: 
org: {student1,student2,student3} 
text: {student3,student4,student1,student5} 
both arraylists are of different sizes. 
I want to use an if statement or some logic that can results in the results as: 
outList: {student1,student3} 

i.e.I want to add only the values which are present in both the lists, into a new list. Something like this:

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    April-22-2015 7:37 AM

You could have an outer loop that loops through text and an inner loop that loops through orgId and inside that, compare the two items to see if they are equal.

for( String[] a1 : text )
    for( String[] a2 : objId )
        if( a1[0].equals(a2[0]) )
            // do something

Don't want to miss updates? Please click the below button!

Follow MindStick