Complete this method in Java Add to this set any element of
Complete this method in Java
Add to this set any element of another set that is not already in this set.
* The result is this set unioned with the other set.
@precondition * The parameter, set2, is not null.
@postcondition * The elements from set2 have been unioned with this set.
public void add(IntSet set2)
{
int[] arr = set2.toArray(); //first thing to convert set to array
//complete this method
}
Solution
public void add(HashSet<Integer> set2)
{
int count;
if(this!=null && set2!=null)
{
int[] arr=set2.toArray();
int[] arr1=this.toArray();
for(int i=0;i<arr1.length;i++)
{
count=0;
for(int j=0;j<arr.length;j++)
{
if(arr[i]==arr1[j])
{
count++;
}
}
if(count==0)
{
this.add(arr[i]);
}
}
}
else System.out.println(\" null set\");
}

