I'm making a program that constructs a set that takes in a variety of objects. However, when I try to clone the set I'm getting CloneNotSupportedException, despite declaring CloneNotSupportedException and implementing the Cloneable interface.
Here's the code,
import java.util.ArrayList;
public class NewSet implements Cloneable {
private ArrayList<Object> objects;
public NewSet() {
this.objects=new ArrayList<Object>();
}
public void add(Object b) {
if(this.contains(b)) {
return;
}
else {
objects.add(b);
}
}
public boolean contains(Object h) {
for(int x=0; x<this.size(); x++) {
if(this.get(x)==h) {
return true;
}
}
return false;
}
public Object get(int i) {
return objects.get(i);
}
public int size() {
return objects.size();
}
public Object clone() throws CloneNotSupportedException {
NewSet copy= (NewSet) super.clone();
return copy;
}
public static void main(String[] args) {
NewSet mb= new NewSet();
mb.add("b");
mb.add("c");
mb.add("d");
Object mc=mb.clone();
}
}
Any help would be appreciated.