Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two fixed list

Status
Not open for further replies.

bostero

Programmer
Apr 17, 2008
3
CA
I have 2 fixed list and I need to compare if an element on list one it is also contained on list two. Do I have to use compareList? or equals()?

this is what I have in mind
if(list1==list2)
return true;
else
return false;
 
Code:
import java.util.ArrayList;
public class ColTest{
public static void main(String args[]){
              String one = new String("1");
              String two = new String("2");
              ArrayList ar1 = new ArrayList();
              ar1.add(one);
              ar1.add(two);
              ArrayList ar2 = new ArrayList();
              ar2.add(two);
              ar2.add(one);

              int matchCount = 0;
              for (int i=0; i<ar1.size(); i++){
                  if ( ar2.contains(ar1.get(i)) ){
                     matchCount++;
                  }
              }
              System.out.println("matchCount"+matchCount);
              if (matchCount==ar1.size()){
                 System.out.println("All elements are found in Arraylist 2");
              }
       }     
       
}
 
I'd use the java.util.Arrays class: just sort and compare.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top