I am just trying to add some objects to an ArrayList in eclipse, but i keept getting an error (Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList) under the 'persons.add(one);'. Any idea what I am doing wrong?
package thequestion;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class PersonComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return 0;
}
Person one = new Person("Kevin", "Gresmer");
List<Person> persons = new ArrayList<Person>();
persons.add(one);
public void sortByLastName(List people) {
Comparator comp = new PersonComparator();
Collections.sort(people, comp);
}
}
public class Person {
private String firstName = null;
private String lastName = null;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
persons.add(one);statement isn't within a method. Only declarations can be directly within a class.persons.add(one);should be invoked?personsat all here?)