Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;找不到符号-方法“添加”;使用ArrayList_Java_Methods_Arraylist - Fatal编程技术网

Java &引用;找不到符号-方法“添加”;使用ArrayList

Java &引用;找不到符号-方法“添加”;使用ArrayList,java,methods,arraylist,Java,Methods,Arraylist,我正在尝试创建一种方法,允许将另一个帐户添加到集合中: import java.util.*; import java.util.ArrayList; /** * The Account list if the grouping of all the accounts for customers in the system. * * @author * @version 1.0 */ public class AccountList { // This is the Arr

我正在尝试创建一种方法,允许将另一个
帐户添加到集合中:

import java.util.*;
import java.util.ArrayList;

/**
 * The Account list if the grouping of all the accounts for customers in the system.
 * 
 * @author
 * @version 1.0
 */
public class AccountList
{
    // This is the ArrayList being declared

    private ArrayList<Account> accounts;

    /**
     * Constructor for objects of class AccountList
     */

    public AccountList()
    {
        //This is the ArrayList being initialised in a constructor.
        accounts = new ArrayList<Account>() ;
    }

    /**
     * This method will allow a new account to be added to the system.
     * 
     * @param accounts the accounts in the system.
     */
    public void addAccount(Account accounts)
    {
        accounts.add();

    }
}
import java.util.*;
导入java.util.ArrayList;
/**
*帐户列表(如果为系统中客户的所有帐户分组)。
* 
*@作者
*@version 1.0
*/
公共类帐户列表
{
//这是正在声明的ArrayList
私人ArrayList账户;
/**
*AccountList类对象的构造函数
*/
公共帐户列表()
{
//这是在构造函数中初始化的ArrayList。
accounts=newarraylist();
}
/**
*此方法将允许向系统中添加新帐户。
* 
*@param帐户系统中的帐户。
*/
公共无效账户(账户)
{
accounts.add();
}
}
问题是,即使在类的顶部导入了
ArrayList
类,它也无法在
addAccount
部分找到方法
add
。我是Java新手,因此非常感谢您的帮助

您的
void addAccount(Account accounts)
方法接受一个名为
accounts
Account
类型的参数,我假设您的
Account
类没有
add
方法,因此您得到的错误与
ArrayList
的add方法无关

应该是:

public void addAccount(Account account)
{
    accounts.add(account);
}
假设您希望将单个帐户添加到帐户列表中

您的错误是使用了相同的变量名
accounts
作为方法的参数和持有列表的成员。前者隐藏了后者,此外,您没有向
ArrayList的add方法提供参数

public void addAccount(Account account)
{
    accounts.add(account);
}
如果使用与外部变量相同的局部变量名称。然后首先考虑局部变量,

代码

的变化
public void addAccount(Account accounts)
{
    this.accounts.add(accounts);

}

更多信息

ArrayList
没有不带任何参数的
add
方法。我想你遇到了好的老“阴影”。您的
accounts.add()
操作不在类的
accounts
字段上,而是在methods参数上。