java - 为什么将对象添加到 ArrayList 时出现此错误 - Java

标签 java oop error-handling arraylist

我正在尝试向我的 ArrayList friend 添加对象...我收到此错误,

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

我正在尝试添加我的 person 对象的一些实例,以最终创建一棵 friend 树。

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;  
    }
}

错误位于“add”一词下方。我还想知道如何命名对象的实例,因此我只调用名称而不是两个属性。

预先感谢您的所有帮助。

最佳答案

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();
    }
}

关于java - 为什么将对象添加到 ArrayList 时出现此错误 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799429/

相关文章:

validation - Scala中的错误处理单子(monad)?尝试与验证

ruby - else block 中的Ruby异常处理

node.js - 在保留原始错误的 Node 中运行 next(new Error())

java - 窗口打开但不响应 OpenGL 3.2

java - 如何访问 "The full stack trace of the root cause is available in the Apache Tomcat/8.0.39 logs"

php - 对于单个开发人员构建的应用程序来说,php 接口(interface)是多余的吗?

java - 有没有办法确定类在 Java 中是什么类型的实例?

PHP/MySQL : Database class and dependency injection

java - 我想从 Java 中的 REST API 以 XML 形式返回响应

java - 将二进制字符串转换为字符