0
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.

6
  • Quick question about your requirement - what should be returned if the list contains a duplicate that's different from the element you're inserting? Commented Apr 11, 2017 at 2:18
  • it should return true in that case@DavidWallace Commented Apr 11, 2017 at 2:38
  • 1
    So all you need to do is use contains to 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. Commented Apr 11, 2017 at 2:47
  • @DavidWallace He said that 'it's not working with our code' which I don't believe. Commented Apr 11, 2017 at 3:06
  • Of course, OP's code mentions a class called Instructor (in main) but they've shown us a class called list. I suspect OP is confused about which class is which, @JacobG. Commented Apr 11, 2017 at 3:19

5 Answers 5

6

You can simply utilize ArrayList#contains to verify if an element already exists within the List.

public boolean addCourse(String course) {
    if (courses.contains(course)) {
        return false;
    }

    return courses.add(course);
}
Sign up to request clarification or add additional context in comments.

2 Comments

it should return true if we are inserting new element in arraylist.but its not working with our code @jacob G
@user7799918 That's exactly what this does.
1

You are adding course in the list and then iterating thr the list, so it always gives you true. ArrayList allows duplicates.

 if(courses.contains(value))

will always return true as you are adding the course before this in arraylist.

Suggestion: You should use Set than list if you want to avoid duplicates.

Comments

1

Instead of using ArrayList, how about using HashSet to keep your courses ?

http://beginnersbook.com/2013/12/hashset-class-in-java-with-example/

Comments

1

Try the code below:

import java.util.ArrayList;
public class list {
  protected ArrayList<String> courses = new ArrayList<String>();
  protected String temp = "";

  public list(String str, String str2) {

  }

  public boolean addCourse(String course) {
    boolean a = true;
    if (courses.isEmpty()) {
        courses.add(course);
        temp = course;
    } else {
        if (temp.equalsIgnoreCase(course)) {
            a = false;
            temp = "";
        } else {
            a = true;
            courses.add(course);
            temp = course;
        }
    }
    return a;
  }
  public static void main(String[] args) {
    list inst = new list("John", "WIU");
    System.out.println(inst.addCourse("CS560"));
    System.out.println(inst.addCourse("CS500"));
  }
}

Comments

0

Simple way you can do:

For Each time executing else block so. remove else block it will work.

for(String value : courses)
 {
        if(courses.contains(value))
        {
         a=false;
         break;
        }

            a=true;

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.