Need to finish implementing this to use UseSet class. Not sure if what I have implemented is 100% correct.
However I need help with the Union and SysDiff.
public class Set
{
private ArrayList<Integer> elements;
public Set()
{
elements = null;
}
public Set(ArrayList<Integer> s)
{
int i;
elements = new ArrayList<Integer>();
for(i=0; i<s.size(); i++)
elements.add(s.get(i));
}
public Set(int[] s)
{
int i;
elements = new ArrayList<Integer>();
for(i=0; i<s.length; i++)
elements.add(s[i]);
}
public String toString()
{
//implement this method
}
public boolean isElement(int elt)
{
int i
for (i=0; i < elements.size(); i++)
{
if (elements.get(i) == elt)
return true;
}
return false
}
public int cardinality()
{
return elements.size();
}
public Set intersect(Set s)
{
Array list <interger> iset = new Array(ist<interger>();
int i;
for (i=0; i<elements.size(); i++)
{
if (s2.isElement (elements.get(i)))
iSet.add(elements.get(i)));
}
return new set(iset)
}
public Set union(Set s)
{
//implement this method
}
public Set symDiff(Set s)
{
//implement this method
}
Have you considered using one of the Java-provided classes, such as TreeSet? Most of the basic set operations can be implemented far more easily using such a class as a starting point.
For example:
Your isElement() method is named contains() in Set/TreeSet
cardinality() is size()
intersect can be implemented using retainAll()
union() can be implemented using addAll()
symDiff() can be implemented using removeAll() to remove the intersection elements from the union of two sets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With