[英]-accountId cannot be resolved or is not a field
I can't figure out what's the problem with my method, I have an ArrayList named accounts which I'm trying to implement into this other class' method. 我无法弄清楚我的方法出了什么问题,我有一个名为accounts的ArrayList,我试图将其实现到另一个类的方法中。 I'm getting this errormessage: "Multiple markers at this line -accountId cannot be resolved or is not a field -accountId cannot be resolved or is not a field"
我收到此错误消息:“此行上的多个标记-accountId无法解析或不是字段-accountId无法解析或不是字段”
Why doesn't it work? 为什么不起作用?
public boolean deposit(long pNr, int accountId, double amount){
for(int i = 0; i < customerlist.size(); i++)
{
if(this.pNr == pNr)
{
for(int j = 0; j < accounts.size(); j++)
{
if(accountId == accounts.accountId)//problem seem to be here
{
balance = balance + amount;
}
}
}
else
return false;
}
return true;
}
accounts
seems to be a List, so you should try to access the field of an item of this List instead of accessing the item of the List itself. accounts
似乎是一个列表,因此您应该尝试访问此列表项的字段,而不是访问列表本身的项。
if(accountId == accounts.get(j).accountId)
{
balance = balance + amount;
}
And you should iterate over your list with an Iterator
or a foreach - then you don't have to access every item manually. 而且您应该使用
Iterator
或foreach在列表上进行Iterator
-这样就不必手动访问每个项目。
I've made some changes, so you have to adapt the following piece to fit in your code: 我进行了一些更改,因此您必须调整以下内容以适合您的代码:
public boolean deposit(long pNr, int accountId, double amount){
for(Customer customer : customerlist)
{
if(pNr == customer.getPNr())
{
for(SavingsAccount account : accounts)
{
if(accountId == account.getAccountId())
{
balance += amount;
return true;
}
}
return false;
}
}
return false;
}
If you want to access private fields of other objects, you have to use a getter (like getAccountId()
or getPNr()
) - these are methods which just return "their" private field. 如果要访问其他对象的私有字段,则必须使用getter(如
getAccountId()
或getPNr()
)-这些方法仅返回“其”私有字段。
You should try 你应该试试
//for lists
if(accountId == accounts.get(j).accountId)
//for arrays
if(accountId == accounts[j].accountId)
On the problem line. 在问题线上。
Your current code is trying to get the accountId of the list not of the listitem. 您当前的代码正在尝试获取列表的accountId,而不是listitem。 Using get() (or [] if it's an array) will select the item in the list of that index.
使用get()(如果是数组,则使用[])将在该索引列表中选择项目。
Using my magic crystal ball, I'm going to say that accounts
is of type List<Account>
. 使用我的魔幻水晶球,我要说
accounts
的类型为List<Account>
。 You need to get the account at position j
. 您需要在第
j
位获得该帐户。
Example
例
if(accountId == accounts.get(j).accountId) {
}
NOTE : You shouldn't be exposing accountId
. 注意 :您不应该公开
accountId
。 This should be retrieved via an accessor method, so it should read something like: 应该通过访问器方法来检索它,因此它应该显示为:
if(accountId == accounts.get(j).getAccountId()) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.