搜索在ArrayList中与某些字符串自定义对象 [英] Searching in a ArrayList with custom objects for certain strings
本文介绍了搜索在ArrayList中与某些字符串自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义对象的ArrayList一个。我想这个ArrayList的弦乐内进行搜索。
I have a ArrayList with custom objects. I want to search inside this ArrayList for Strings.
为对象的类是这样的:
public class Datapoint implements Serializable {
private String stateBased;
private String name;
private String priority;
private String mainNumber;
private String groupadress;
private String dptID;
public Datapoint(){
}
public String getMainNumber() {
return mainNumber;
}
public void setMainNumber(String mainNumber) {
this.mainNumber = mainNumber;
}
public String getName() {
return name;
}
..and so on
我知道如何搜索字符串中的ArrayList,但如何做,在一个ArrayList中与我的自定义对象:
I know how to search for a string in a ArrayList but how to do that in a ArrayList with my custom objects:
ArrayList<String> searchList = new ArrayList<String>();
String search = "a";
int searchListLength = searchList.size();
for (int i = 0; i < searchListLength; i++) {
if (searchList.get(i).contains(search)) {
//Do whatever you want here
}
}
所以我想有一个函数在我的ArrayList与所有的名的字符串,例如五个对象进行搜索。
So I want to have a function to search in my ArrayList with for example five objects for all "name" strings.
推荐答案
最简单的办法就是让为
在那里你验证,如果atrrtibute 名称
的自定义对象具有所需的字符串
The easy way is to make a for
where you verify if the atrrtibute name
of the custom object have the desired string
for(Datapoint d : dataPointList){
if(d.getName() != null && d.getName().contains(search))
//something here
}
我认为这可以帮助你。
I think this helps you.
这篇关于搜索在ArrayList中与某些字符串自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文