我正在进行一项任务,并让此代码抛出一个无法找到符号的错误
arraylist在方法之外声明,它在if语句之前的for循环中工作 .
我正在尝试搜索arraylist以获取员工的姓氏,然后将索引返回到main方法以进行进一步处理 .
public int searchEmployee(String lastN)
{
int noName = 0;
int lastNIndex = 0;
for (int i = 0; i < employeeList.size(); i++)
{
if (employeeList(i).getLastName().equalsIgnoreCase(lastN))
{
lastNIndex = i;
}
else
{
i = noName;
}
}
lastNIndex = noName;
return lastNIndex;
exit status 1
Directory.java:74: error: cannot find symbol
if (employeeList(i).getLastName().equalsIgnoreCase(lastN))
^
symbol: method employeeList(int)
location: class Directory
1 error
3 回答
ArrayList搜索
https://beginnersbook.com/2013/12/java-arraylist-get-method-example/
尝试employeeList.get(i)而不是employeeList(i)
您应该使用方法 get 来检索列表中指定位置的元素 . 有关详细信息,请参阅https://docs.oracle.com/javase/8/docs/api/java/util/List.html#get-int-
固定版本的代码: