Hi, I'm having trouble with this exercise. Any help would be appreciated. I think I only have one part wrong, but I'm not sure. I think it has to do with checking for x in ArrayList a. I'm pretty sure I'm not checking for "x" correctly. The error I'm getting is "not a statement: for (x : a) {" or when I put "Person x : a" I get an error that says "modifier expected"
Here is the exercise instructions and prewritten code:
In the following definition of the Person class, complete the definition of the includeMeIn method. This takes as its only argument an ArrayList a that contains nothing but Person objects. It adds the instance to the ArrayList, provided only that there is not already a Person object in the ArrayList with the same name: public class Person { private String myName; public Person( String name ) { myName = name; } public String getName() { return myName; } //my code would go here //here is what I have so far: public void includeMeIn( ArrayList<Person> a ) { for (Person x : a){ if (Person.new == x){ a.remove(x); } } } } public class MainClass { public static void main( String[] args ) { //and here is some other prewritten code that I can modify: ArrayList<Person> people = new ArrayList<Person>(); Person p1 = new Person( "fred" ); Person p2 = new Person( "eric" ); Person p3 = new Person( "jason" ); Person p4 = new Person( "davina" ); Person p5 = new Person( "eric" ); p1.includeMeIn( people ); p2.includeMeIn( people ); p3.includeMeIn( people ); p4.includeMeIn( people ); p5.includeMeIn( people ); for ( Person p : people ) System.out.println( p.getName() ); //That part was modifiable } }