Reputation: 191
Idea is to calculate union of two collection sets,at the begining of the program,user will be asked to enter the length of desired sets,afterwards he will be prompted to assign the elements in sets,and the final idea is to calculate Union. I have reached the very end and after compiling my program only elements of the first set are printed,I really don't know why.
So my question is how can I calculate the sets union,following the idea I started.
MY OUTPUT:
Desired array lengths: 3 3 First set elements: 1 2 3 Second set elements: 4 5 6 UNION of our sets: 1.002.003
public class Union{
public static void main(String[] args) {
System.out.println("Desired array lengths: ");
Scanner scan = new Scanner(System.in);
//Infinite loop for reading input,if negative number entered break the loop!
while (true) {
int n1 = scan.nextInt();
int n2 = scan.nextInt();
if (n1 < 0 || n2 < 0)
break;
// Assigning elements to the first set.
double[] s1 = new double[n1];
System.out.println("First set elements: ");
//We enter elements as long as the number of the elements added is less than the length of an array we assigned.
for (int i = 0; i < n1; s1[i++] = scan.nextInt());
if (n1 == 0)
System.out.println();//If we do not enter any element go to new line
//Assigning elements to the second set.
double[] s2 = new double[n2];
System.out.println("Second set elements: ");
for (int i = 0; i < n2; s2[i++] = scan.nextInt());
if (n2 == 0)
System.out.println();//Same as before.
// Calculating union
double[] s3 = new double[n1 + n2];//We reserve memory space for the s3 array with the size equal to both n1 and n2 arrays.
int n3 = 0; // Variable used to save number of elements,after reaching the end of the loop n3 WILL HAVE THE SIZE OF N1.
while (n3 < n1)
s3[n3] = s1[n3++];
for (int j = 0; j < n2; j++) { //HERE WE ARE CHECKING IF THE ELEMENTS FROM N2 SET ARE PRESENT IN THE N1
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
double[] pom = new double[n3];
for (int i = 0; i < n3; pom[i] = s3[i++]);
s3 = pom;
pom = null;
System.out.println("UNION of our sets: ");
for (int i = 0; i < n3; System.out.printf("%.2f", s3[i++]))
;
System.out.print("\n\n");
}
}
Upvotes: 2
Views: 1004
Reputation: 30819
It's because of the following block:
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
It's trying to compare the elements of two arrays with similar indices, e.g. if first element of s1
and s2
are not equal, control will break out of while
loop and hence, i
will never be n1
, resulting in element at index j
of s2 bein skipped.
As far as calculation of union is concerned, you can easily do it with Set
in Java, e.g.:
Set<Double> elements = new LinkedHashSet<>();
for(double number : s1){
elements.add(number);
}
for(double number : s2){
elements.add(number);
}
double[] union = new double[elements.size()];
int i = 0;
Iterator<Double> iterator = elements.iterator();
while(iterator.hasNext()){
union[i++] = iterator.next();
}
Upvotes: 1
Reputation: 379
Can you use the Java Collections Framework? The Set interface can do this very easily.
Set<Double> union = new HashSet<>();
for (double e : s1)
union.add(e);
for (double e : s2)
union.add(e);
System.out.println(union);
If you cannot use the collections framework and have to roll your own then you might consider rolling your own binary tree or heap.
Upvotes: 1
Reputation: 8292
The mistake lies in the code where you are checking which elements of set
s2
, you need to put in set
s3
.
Basically you need to check whether an element of set
s2
is present in set
s1
.
So change following code:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] == s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
to this code:
for (int j = 0; j < n2; j++) {
int i = 0;
while (i < n1 && s2[j] != s1[i])
i++;
if (i == n1)
s3[n3++] = s2[j];
}
The loop while (i < n1 && s2[j] != s1[i])
will terminate with i = n1
, only when none of the elements in set
s1
, matches with element s2[j]
and this is the element which we want in the UNION
of 2 sets.
Upvotes: 1