import java.util.ArrayList;
public class list {
protected ArrayList<String> a = new ArrayList<String>();
public boolean ad(String aa)
{
boolean t=true;
a.add(aa);
for(String value : courses)
{
if(a.contains(value))
{
a=false;
}
else
{
a=true;
}
}
return a;
}
}
this program should return false if arraylist course contains duplicate elements.else if we are inserting new element return true.
expected output for above code is
true
but it only returns false for any condition.
containsto check whether the list contains the element you're adding, right? No need for a loop! But it has to be before you add the element, otherwise you'll just find the element itself. Jacob G. has posted the solution you're looking for.Instructor(inmain) but they've shown us a class calledlist. I suspect OP is confused about which class is which, @JacobG.