HOW TO COMPARE TWO ARRAYLISTS OF TYPE ARRAYLIST<STRING[]>?

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
 915  View(s)
Ratings:
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<>() ;
 
System.out.println();
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);
        System.out.println(orgId.get(i)[x]);
    }
}
System.out.println();
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);
        System.out.println(text.get(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:
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: how to compare two ArrayLists of type ArrayList<String[]>?

    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
            }
        }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!