• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Compare two ArrayList

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI ,
See the below code, I am comparing two arraylist values and getting the correct result while comparing equal. in bello example
I took the values ArrayList a1=a,b,c,d,e and ArrayList a2=a,b,c.
If I compare a1==a2 result : a,b,c is comming correctly. if I try to compare not equal position ,I want to get the data d,e. If any body know please modify the code and send me back.

Thank you advanced..

ArrayList a1=new ArrayList();
ArrayList a2=new ArrayList();
a1.add("a");
a1.add("b");
a1.add("c");
a1.add("d");
a1.add("e");
a2.add("a");
a2.add("b");
a2.add("c");

for(int i=0;i<a1.size();i++){
String str1 = (String) a1.get(i);
for(int j=0;j<a2.size();j++){
String str2 = (String) a2.get(j);
//int result = str1.compareTo(str2);
// if (result == 0)
//System.out.println(str1+ " = " + str2);
//else if (result > 0)
//System.out.println(str1+ " > " + str2);
if (str2.equalsIgnoreCase(str1))
System.out.println(str1);

}

}
 
Ranch Hand
Posts: 56
Tomcat Server Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code produces what you want and a little more, what it should do is take any two arraylists check which one has more elements in it. Go through both for the amount of elements in the smallest, comparing element for element between the arrays and displaying the ones which don't match.

Then goes through the remainder of the larger array and displays all since obviously there isn't a match because the smaller array has no elements for that range.

Next time, instead of asking for all the code perhaps you should ask for advice, better if you do it yourself, then you actually learn something.

 
Bindu Sanju
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael Labuschagne

Its working fine and thanks for your suggestion.

Regards,
Sanju.
 
The moustache of a titan! The ad of a flea:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic