469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

Comparing Two Lists for common elements

JoeMac3313
My Assignment was to compare two lists and print out the number of elments that are same.

It is supposed to look like this

Week 7 Homework
The number of common elements is: 3
The number of common elements is: 3

I get

Week 7 Homework
The number of common elements is: 0
The number of common elements is: 1

Which is better than my many other attempts, if you could offer any help I would appreciate it.

JoeMac3313


Expand|Select|Wrap|Line Numbers
  1. import ch06.lists.*;
  2.  
  3. public class CompareListsTest
  4. {
  5.  
  6.    private static int compareUnsortedLists(UnsortedListInterface list1, UnsortedListInterface list2) {
  7.  
  8.     int counter = 0;
  9.  
  10.         list1.reset();
  11.         list2.reset();
  12.  
  13.         if(list2.contains(list1.getNext())&& list1.getNext() != null)
  14.         {    
  15.             counter++;
  16.         }
  17.  
  18.     return counter;
  19.  
  20.    }
  21.  
  22.  
  23.    public static void main(String[] args) {
  24.  
  25.       // declare and populate list lst1
  26.       UnsortedListInterface lst1 = new RefUnsortedList();
  27.     lst1.add("alpha");
  28.     lst1.add("tau");
  29.     lst1.add("beta");
  30.     lst1.add("kappa");
  31.     lst1.add("gamma");
  32.     lst1.add("delta");
  33.  
  34.      // declare and populate list lst2
  35.      UnsortedListInterface lst2 = new RefUnsortedList();
  36.         lst2.add("tim");
  37.         lst2.add("gamma");        
  38.         lst2.add("beta");
  39.         lst2.add("peter");
  40.         lst2.add("kappa");
  41.  
  42.      // Self introduction
  43.      System.out.println("Week 7 Homework");
  44.  
  45.      // Call compareUnsortedLists
  46.      System.out.println("The number of common elements is: " + compareUnsortedLists(lst1, lst2));
  47.  
  48.      // The property should be symetric and therefore the same result should be
  49.      // obtained if the parameters are swapped in the method call
  50.      System.out.println("The number of common elements is: " + compareUnsortedLists(lst2, lst1));
  51.  
  52.   }
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
Jul 14 '07 #1
3 34332
JosAH
11,448 Expert 8TB
If your lists contain n and m elements respectively you have to perform n*m
comparisons. If the elements nicely implement the equals() and hashCode()
methods you can the lists figure out the presence of an element. It would clean
up your code enormously:

Expand|Select|Wrap|Line Numbers
  1. List<YourType> listA= ...
  2. List<YourType> listB= ...
  3. int common= 0;
  4. for (YourType element: listA) 
  5.    if (listB.contains(element))
  6.       common++;
  7.  
kind regards,

Jos
Jul 14 '07 #2
I fixed it w/ this code. Thanks for the help!

Expand|Select|Wrap|Line Numbers
  1.  
  2. int counter = 0;
  3. int count = 0;
  4.  
  5.     list1.reset();
  6.  
  7.     while(counter < list1.size())
  8.     {
  9.         if(list2.contains(list1.getNext()))
  10.         {
  11.             count++;
  12.         }
  13.  
  14.         counter++;
  15.     }
  16.  
Jul 14 '07 #3
JosAH
11,448 Expert 8TB
I fixed it w/ this code. Thanks for the help!
Good; you're welcome and best of luck with your Java projects.

kind regards,

Jos
Jul 14 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

29 posts views Thread by Joseph Haig | last post: by
9 posts views Thread by Dave H | last post: by
41 posts views Thread by Odd-R. | last post: by
1 post views Thread by Grzegorz ¦lusarek | last post: by
1 post views Thread by Nishant | last post: by
11 posts views Thread by Sheldon | last post: by
12 posts views Thread by rshepard | last post: by
3 posts views Thread by Sean Dalton | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.