Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda