我在类中有一个数组列表,数组名为realtorList。从我的主类我将具有房地产经纪人数据的对象存储到realtorList。 我的数据存储在文本文件中,并在第一行中读取。 这是我存储第一行数据后realtorList中的第一个元素。
[房地产经纪人{licenseNumber = AA1111111 ,firstName = Anna,lastName = Astrid,phoneNumber = 111-111-1111, 佣金= 0.011}]
当我从输入文件中读取下一行数据时,我需要查看realtorList中是否已存在以粗体显示的licenseNumber。我无法弄清楚如何去做这件事。
例如,如果下一个房地产经纪人数据许可证号是AA1111111,我如何检查此示例中存在的AA1111111的realtorList。
答案 0 :(得分:1)
执行此操作的一种非常简单的方法是使String ArrayList并行运行(例如,一个称为许可证),如果该许可证值已在List中,则使用带有indexOf的if语句返回。由于许可证ArrayList只有一个值,因此可以使用indexOf轻松搜索。
一个例子是
Specifier Argument Type Output format
--------- ------------- -------------
%d,%i int decimal integer
%u unsigned int decimal integer (non-negative)
%f double decimal floating point
%x,%X unsigned int hexadecimal integer (non-negative)
%o unsigned int octal (non-negative)
%c char single character
%s char * text
类似的代码适用于我的一个项目,在这个项目中,机器人的动态电机列表会在添加新端口之前检查是否已经有列出端口的电机。
另一种方法可以使用for循环进行线性搜索,例如
private boolean checkLicense (String licenseNumber) {
int i = licenses.indexOf(licenseNumber);
if(i == -1) {
return false;
} else {
return true;
}
}
这将对每个对象执行线性搜索,直到找到它为止(它需要在上面示例中的方法中以这种方式工作)