0

I am trying to add objects to my ArrayList friends... I get this error,

The method add(int, Person) in the type ArrayList is not applicable for the arguments (int, String)

I am trying to add a few instances of my person object to eventually create a tree of friends.

import java.util.*;

public class Person
{
    public int     id;     // some identification number unique to the person
    public boolean zombie; // true if the person is a zombie
    public char    state;  // p means human, z means zombie

    public static ArrayList<Person> friends;  // list of friends

    public Person(int id, char state)
    {
        this.id = id;
        this.state = state;
        //this.zombie = zombie;
    }

    public static void addPeople() 
    {
        friends = new ArrayList<Person>();
        friends.add(1, 'p');
    }

    public boolean isZombie() 
    {
        if (state == 'p')
        {
            return zombie=false;
        }
        else if (state == 'z')
        {
            return zombie=true;
        }

        return zombie;  
    }
}

The error is located under the "add" word. I would also like to know how I can name the instances of the object so I only call the name rather than the two attributes.

Thanks in advance for all help.

1
  • don't reinitialize friends every time, you delete old entries. Commented Apr 3, 2013 at 22:44

2 Answers 2

3
import java.util.ArrayList;
import java.util.List;

/**
 * Person description here
 * @author Michael
 * @link http://stackoverflow.com/questions/15799429/why-am-i-getting-this-error-when-adding-object-to-arraylist-java/15799474?noredirect=1#comment22468741_15799474
 * @since 4/3/13 9:45 PM
 */
public class Person {
    private Integer id;
    private boolean zombie;
    private List<Person> friends;

    public static void main(String [] args) {
        List<Person> lastPeopleStanding = new ArrayList<Person>();
        for (int i = 0; i < 3; ++i) {
            lastPeopleStanding.add(new Person(i));
        }
        lastPeopleStanding.get(0).addFriend(lastPeopleStanding.get(1));
        lastPeopleStanding.get(0).addFriend(lastPeopleStanding.get(2));
        System.out.println(lastPeopleStanding);
    }

    public Person(Integer id) {
        this.id = id;
        this.zombie = false;
        this.friends = new ArrayList<Person>();
    }

    public boolean isZombie() { return this.zombie; }

    // Irreversible!  Once you go zombie, you don't go back
    public void turnToZombie() { this.zombie = true; }

    // Only add a friend if they're not a zombie
    public void addFriend(Person p) {
        if (p != null && !p.isZombie()) {
            this.friends.add(p);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Person{");
        sb.append("id=").append(id);
        sb.append(", zombie=").append(zombie);
        sb.append(", friends=").append(friends);
        sb.append('}');
        return sb.toString();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I eventually want to make a tree of friends, where Person P would be the root and P can have two friends where one is a zombie and one is a person, and the tree will grow in the same fashion as the first set of friends. Zombie would have two friends one zombie one person along with the person friend. Thanks anyways for the post, really helpful!
+1 for making friends an instance variable and not static, maybe you should use a Set or check if the person is already added as friend. Is there any reason you use Integer instead of int? And please use more moderated language Code as written is worthless we all started out as beginners.
A member variable makes sense because we don't all have the same friends. Set might be a good idea, but you'd better override equals and hashCode properly for Person. Yes, I chose an object rather than primitive because I wanted to be able to make it nullable. It's a common idiom for relational database persistence. Agreed that we all start as beginners, but the coder is not the code. Saying that the code is worthless only makes the coder feel badly if they identify too much with their code. This site can't be that overly sensitive.
how would i add, say Person z? Would I do that in my test class?
Sorry, I don't understand your question. Add a Person to what? The friends array of another Person instance? You can do that in a main, in a test class, whatever.
|
1

You forgot to create a new Person to add to your ArrayList:

From your comment, you can create a class member variable called idCount and increment when a Person is added:

public void addPeople() {
    friends.add(new Person(++idCount, 'p'));
}

Using static methods is generally considered poor design for a class that can have state.

1 Comment

yes but you will have to manage the unique id, say by incrementing the id count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.