473,770 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference of 2 arrays

5 New Member
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays

Ex:

arrayA: 3 5 8 9
arrayB: 3 4 6 9

difference of A-B: 5 8

however, my code is just returning me an array of 0's

im trying to use my search method (which has been tested and works) to take a number from the first array and search it in the other array. If it is not found the search method returns a 0 and the number from the first array is then stored into the 3rd array.

here is the portion of my code, I'm not sure what is wrong.

Expand|Select|Wrap|Line Numbers
  1. //Cory Boughton
  2.  
  3. import java.util.*;
  4.  
  5. public class Sets {
  6.  
  7.     public static Scanner kbd = new Scanner (System.in);
  8.  
  9.     public static final int MAXSIZE = 20;
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         int[]setA = new int[MAXSIZE];
  14.         int[]setB = new int[MAXSIZE];
  15.         int[]intersect = new int [MAXSIZE];
  16.         int[]difference = new int [MAXSIZE];
  17.         int sizeA, sizeB, interSize, diffSize;
  18.  
  19.         System.out.print("How many numbers will be in the 1st set: ");
  20.         sizeA = kbd.nextInt();
  21.         while (sizeA > MAXSIZE){
  22.             System.out.print("Error: Set size is too large. Re-enter set size: ");
  23.             sizeA = kbd.nextInt();
  24.         }
  25.         System.out.println("Enter list of integers for 1st set: ");
  26.         getData(setA, sizeA);
  27.  
  28.         System.out.print("How many numbers will be in the 2nd set: ");
  29.         sizeB = kbd.nextInt();
  30.         while (sizeB > MAXSIZE){
  31.             System.out.print("Error: Set size is too large. Re-enter set size: ");
  32.             sizeB = kbd.nextInt();
  33.         }
  34.         System.out.println("Enter list of integers for 2nd set: ");
  35.         getData(setB, sizeB);
  36.  
  37.         interSize = intersection(setA, sizeA, setB, sizeB, intersect);
  38.         System.out.print("The intersection of the two sets is: ");
  39.         for (int x = 0; x < interSize; x++){
  40.             System.out.print(intersect[x] + " ");
  41.         }
  42.  
  43.         diffSize = difference(setA, sizeA, setB, sizeB, intersect);
  44.         System.out.print("\n\nThe difference of A-B is: ");
  45.         for (int x = 0; x < diffSize; x++){
  46.             System.out.print(difference[x] + " ");
  47.         }
  48.     }
  49.  
  50.     public static void getData(int[]set, int size){
  51.  
  52.         for(int x = 0; x < size; x++){
  53.             int num = kbd.nextInt();
  54.             int count = search(set, size, num);
  55.             if (count == 0)
  56.                 set[x] = num;
  57.             else 
  58.                 x--;
  59.         }
  60.     }
  61.  
  62.     public static int search(int[]set, int size, int num){
  63.  
  64.         int count = 0;
  65.  
  66.         for (int x = 0; x < size; x++){
  67.             if (num == set[x])
  68.                 count++;
  69.         }
  70.         return count;
  71.     }
  72. public static int difference(int[]setA, int sizeA, int[]setB, int sizeB, int[]resultSet){
  73.  
  74.         int y = 0;
  75.         for (int x = 0; x < sizeA; x++){
  76.             int num = setA[x];
  77.             int found = search(setB, sizeB, num);
  78.             if (found == 0){
  79.                 resultSet[y] = num;
  80.                 y++;
  81.             }
  82.         }
  83.         return y;
  84.     }
  85. }
  86.  
Apr 19 '09 #1
11 12122
dmjpro
2,476 Top Contributor
@cmb3587
On the basis of which logic you are saying the difference ?
Apr 20 '09 #2
JosAH
11,448 Recognized Expert MVP
@dmjpro
From ordinary set theory: A-B are all elements in A that are not in B.

kind regards,

Jos
Apr 20 '09 #3
r035198x
13,262 MVP
Why not have a look at the ArrayList class and see what magic you can conjure up with it?
Apr 20 '09 #4
dmjpro
2,476 Top Contributor
See you better to use Collection instead of Array.
If you use Collection then you will find better APIs to do your work easily.

Expand|Select|Wrap|Line Numbers
  1. List set_a;
  2. List set_b;
  3. List set_c;
  4.  
  5. for(int i=0;i<set_a.size();i++){
  6.  if(set_b.indexOf(set_a.get(i))!=-1) set_c.add(set_a.get(i));
  7. }
  8.  
Apr 20 '09 #5
r035198x
13,262 MVP
@dmjpro, if you keep looking at the API you will find cleaner and even easier ways of doing it.
Apr 20 '09 #6
dmjpro
2,476 Top Contributor
@r035198x
Sorry i couldn't find it any? ;)
Apr 20 '09 #7
r035198x
13,262 MVP
For starters you could have used the contains method.

For blatant cheaters you could look at retainAll combined with removeAll.
Apr 20 '09 #8
dmjpro
2,476 Top Contributor
Ahh! actually "retainAll" method stroke my brain.
Anyway what i could figure out ...

Expand|Select|Wrap|Line Numbers
  1. List set_a;
  2. List set_b;
  3.  
  4. List temp_set = set_a.clone();
  5. temp_set.retainAll(set_b); //it finds out the common elements between two sets.
  6. set_a.removeAll(temp_set); //now the final desired set is ready
  7.  
That's what you said.
When do you make method call then i think lots of headache is up to RunTime System. What you think which one would be the better solution?
Apr 20 '09 #9
JosAH
11,448 Recognized Expert MVP
@r035198x
I don't understand the 'blatant cheaters' part ...

kind regards,

Jos ;-)
Apr 20 '09 #10

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

Similar topics

2
4305
by: duyifan.nju | last post by:
hello, can someone tell me what is the difference between sort() (in stl) & qsort(stdlib.h) thanks in advance.
79
3432
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same! 9: More about Arrays 10: More about Pointers What blows me out of the water is the fact that 'every' programmer
5
4656
by: Alan Howard | last post by:
We're getting "ERROR (0x8007000E) Not enough storage is available to complete this operation" errors on a fairly large, busy ASP/SQL Server web site. The error is being thrown on a line calling oRs.GetRows() on one of our busiest pages. The array returned from the GetRows() call will be 'cleaned up' when the page goes out of scope, but I wonder if we should be calling Erase specifically after the last usage to explicitly free allocated...
18
2270
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
14
2039
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
2
4659
by: benj | last post by:
Anybody has a good explaination to following question? what is the difference between: int *a and int (*a)
45
7396
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
10
3444
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
3
6392
by: Jeff | last post by:
I've got a series of data like this: Long Sleeve White P/C Sm 32/33 Long Sleeve White P/C Med 32/33 .... What I'd like to do is extract the differences and the similarity. In this case: similar: Long Sleeve White P/C
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.