954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ArrayList: incompatible types.

i have been asked to create a method that is capable of inserting a "Person" class into an ArrayList of persons. but all it says is that the types are incompatible. starting to think that the add method might not work in this situation. any advise would be greatly appreciated! thank you in advance.

import java.util.ArrayList;

public class DikuPlus
{
    private Person person;
    private ArrayList<Person> network;
    
    public void DikuPlus(ArrayList<Person> network)
    {
        this.network = network;
    }
    
    public ArrayList<Person> addPerson(Person person)
    {
        this.network = network.add(person);
    }
}
Mehnad
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Look up the return type of ArrayList.add(). Why are you trying to assign that to your ArrayList variable?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

would this solve the problem? and thank you for being so fast, once again:)

import java.util.ArrayList;

public class DikuPlus
{

    private ArrayList<Person> network;
    
    public void addPerson(Person person)
    {
        network.add(person);
    }
Mehnad
Newbie Poster
5 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Good question. Did it solve the problem? Does the method do what you intend?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You