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参数上。