Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing Two ArrayLists

 
Krish Pinnamaneni
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Krish Pinnamaneni
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeroen Wenting ,
Thanks a lot for your response.
Thanks,
Krish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic