Java 从Arraylist获取项目名称并进行比较
我有一个数组列表,声明为Java 从Arraylist获取项目名称并进行比较,java,arrays,arraylist,Java,Arrays,Arraylist,我有一个数组列表,声明为 ArrayList<Item> items = new ArrayList<Item>(); 分项 public class SectionItem implements Item{ private final String title; public SectionItem(String title) { this.title = title; } public String getTit
ArrayList<Item> items = new ArrayList<Item>();
分项
public class SectionItem implements Item{
private final String title;
public SectionItem(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
@Override
public boolean isSection() {
return true;
}
}
有谁能给我一个更好的处理方法吗?使用,get(int-index)
ArrayList方法。
返回此列表中指定位置的元素
就你而言
String itemTitle = items.get(index).getTitle(); // Here index represent position of item you want to get title from current array list
唯一的方法是使用强制转换:
String sectiontitle=((SectionItem)items.get(index)).getTitle()代码>
不过,在我看来,你应该有一个字符串标题
作为界面项的成员
只需通过访问索引获取标题,如果要比较字符串,请使用equals:
return items.get(index).getTitle().equals(myString);
如果要遍历数组,请执行for循环:
for(int index = 0; index < items.length() -1; index++) {
}
for(int index=0;index
Item的Post-code
class.@user3703050更新的code假设我有一个字符串名“inbox”,如何获得名为inbox的标题。
你能详细说明一下吗?您的意思是如何获得具有该特定标题的项
实例?@copeg-我的意思是,如果预定义了字符串值,则需要与该标题进行比较
for(int index = 0; index < items.length() -1; index++) {
}