my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparing Two ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparing Two ArrayLists" Watch "Comparing Two ArrayLists" New topic
Author

Comparing Two ArrayLists

Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
Hi,
When I run following code i am getting error.Please help me how to make it runnable.
My code is as follows:
_________________________________________________________________________
import java.util.*;

class Test6{
ArrayList a1 ,a2;
Test6(){

a1 = new ArrayList();
a1.add("SriRam");
a1.add("SriKrishna");
a1.add("Gopal");
a1.add("LakshmiPrasad");

a2 = new ArrayList();
a2.add("Potluri");
a2.add("Bommui");
a2.add("Rayaprolu");
a2.add("Ray");




}
public static void main(String[] args){

new Test6().check();



}

public void check(){
// Calling compareEquals(List,List).
Collection c = compareEquals(a1,a2);
System.out.println(c);


}

// I don't want to make changes to compareEquals method.So can any one suggest me how to pass arguments while calling this method from check().

private Collection compareEquals(Object[] as1, Object[] as2) {
int size = as1.length;
Collection al = new ArrayList();

for (int i=0; i<size; i++) {
if (as1[i].equals(as2[i])) {
al.add(Boolean.TRUE);
}
else {
al.add(Boolean.FALSE);
}
}
return al;
}




}

In Advance,
Thanks,
Krish

____________________________________________________________________________Collection c = compareEquals(a1,a2);
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You have to turn the Lists into arrays. There are methods for this in the List interface and thus in the concrete List implementation classes.

Two avenues of approach suggest themselves.
Either do the conversion in each call to the compare method or override the method with a version taking 2 Lists and have that overridden version do the conversion and return the result of the original one using the converted Lists as arguments.


42
Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
Hi Jeroen Wenting ,
Thanks a lot for your response.
Thanks,
Krish
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comparing Two ArrayLists
 
Similar Threads
removing duplicates from the list?
Eliminating Duplicate in Arraylist and moving the dups to another arraylist.
Constructors question
ArrayList
Read file from directory, update contents of the each file