I am trying to implement my own Set with an ArrayList
in Java. I have written the method add()
which is responsible for adding a new element in the Set, the method contains
, which returns true if an element is found in the Set and the method addAll
, which I want to add all the elements which the collection in the parameter contains in the Set. Despite that, when I create two objects of the class Set and invoke the method on the i get an error message on Eclipse saying : The method addAll(Collection) in the type Set is not applicable for the arguments Set
.This is the code I have written for the implementation so far:
import java.util.ArrayList;
import java.util.Collection;
public class Set<E>{
private ArrayList<E> List = new ArrayList<>();
public boolean add(E e) {
if(contains(e))
return false;
return List.add(e);
}
public boolean addAll(Collection<E> e) {
for(E i : e)
{
if(!List.contains(i)) {
List.add(i);
}
}
return true;
}
public boolean contains(E e) {
return List.contains(e);
}
This is the class from which I instantiate the objects and the method invoking I do on them:
public class NewClass
{
private Set setOne;
private Set setTwo;
public static void main(String[] args) {
setOne.addAll(setTwo);
}
Can someone help me understand what I am doing wrong? Thanks in advance