• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Comparing Two ArrayLists

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeroen Wenting ,
Thanks a lot for your response.
Thanks,
Krish
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic