Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic