473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 34579
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

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

Similar topics

29
by: Joseph Haig | last post by:
I am trying to use descriptive lists, <DL>, as shown in <http://www.maths.man.ac.uk/~jhaig/tmp/test.html> with a style sheet at <http://www.maths.man.ac.uk/~jhaig/tmp/default-2.css>. With Mozilla...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
1
by: Grzegorz ¦lusarek | last post by:
Hi all. I my application i have situation when i have some lists and i must get from lists common elements. Exacly i don't know how many list I have so at the moment of creation each of one I...
1
by: Nishant | last post by:
Hi there, I am looking forward to your help for comparing matrix in C++ I want to find common elements between 2 or more different matrices,using C++ for eg: A=
11
by: Sheldon | last post by:
Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
3
by: Sean Dalton | last post by:
Hello, I have a two sets OLDLIST and REMOVE. I would like to remove every element in OLDLIST if it is also occuring in REMOVE and store the remaining elements from OLDLIST into NEWLIST. So...
2
by: antar2 | last post by:
Hello, I am a beginner in python. following program prints the second element in list of lists 4 for the first elements in list 4 that are common with the elements in list 5 list4 = ,,]...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.