Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
Method addFriend(Person) is undefined for the type ArrayList<Person>

I have 3 java source files.

  • Person.java
  • FriendsList.java
  • MyFriends.java

FriendsList contains an array list of objects of type Person and a method for adding a new friend to the list.

public class FriendsList{
    ArrayList<Person> friendsList = new ArrayList<Person>(100);
    // Constructor declaration of class
    public FriendsList(ArrayList<Person> friendsList){
            super();
            this.friendsList = friendsList;
    }    
    ...
    public void addFriend(Person friend){
            friendsList.add(friend);
    }
    ...
}

Now, in MyFriends.java I created a new array list of objects of type Person, I declared a new Person and I'm trying to add it to the list using the method addFriend(Person) but I'm getting an error "Method addFriend(Person) is undefined for the type ArrayList<Person>"

public class MyFriends{
    public void main(String[] args){
            ArrayList<Person> friendsList = new ArrayList<Person>(100);            
            Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
            friendsList.addFriend(f1);
    }
}

I tried all other methods defined in FriendsList.java but I get the same error message. I understand that if the source files are in the same folder, the syntax to use a method from the other file is objectName.method(a).

What am I doing wrong?

(Sorry if I'm not asking it right, it's my first question)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I guess that you want to do the following instead:

public class MyFriends{
    public void main(String[] args){
        FriendsList friendsList = new FriendsList();            
        Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
        friendsList.addFriend(f1);
    }
}

You were instantiating an ArrayList<Person> but what you really want is to create an object of your own FriendsList class.


It seems that you don't have a no-args constructor in your FriendsList, so you need to do the following (instantiating a List of Person):

public class MyFriends{
    public void main(String[] args){
        FriendsList friendsList = new FriendsList(new ArrayList<Person>(100));            
        Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
        friendsList.addFriend(f1);
    }
}

Still, in my opinion, you should consider adding a no-args constructor to your FriendsList as follows so that my first suggestion would actually work (thanks @DavidConrad for the hints):

public class FriendsList{
    ArrayList<Person> friendsList = new ArrayList<Person>(100);
    // Constructor declaration of class
    public FriendsList() {
    }  
    
    public FriendsList(ArrayList<Person> friendsList){
            this.friendsList.addAll(friendsList);
    }    
    ...
    public void addFriend(Person friend){
            friendsList.add(friend);
    }
    ...
}
over 4 years ago · Santiago Trujillo Denunciar

0

You are not actually using the FriendsList class. MyFriends should be:

public class MyFriends{
    public void main(String[] args){
            FriendsList friendsList = new FriendsList();            
            Person f1 = new Person("Alice", "Anderson", "519-472-4910", "02", "19");
            friendsList.addFriend(f1);
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda