Java 使用索引遍历arraylist中的项
我想迭代对象的arraylist中的项,同时使用索引,以便将字符串(对象/类的一部分)传输到Java 使用索引遍历arraylist中的项,java,android,loops,arraylist,Java,Android,Loops,Arraylist,我想迭代对象的arraylist中的项,同时使用索引,以便将字符串(对象/类的一部分)传输到arraylist 我的代码: for(PeopleDetails person : people_list; /*not using termination, will terminate at end of people_list */ ;i++){ /// Code here } 但是我得到一个错误,说: 类型不匹配:无法从ArrayList转换为PeopleDetails
arraylist
我的代码:
for(PeopleDetails person : people_list; /*not using termination, will terminate at end of people_list */ ;i++){
/// Code here
}
但是我得到一个错误,说:
类型不匹配:无法从ArrayList转换为PeopleDetails
是什么导致了这个错误?
谢谢你的帮助 这不是每个语句的
(为了清楚起见,我删除了您的评论):-
这是:-
for(PeopleDetails person : people_list)
您使用它就像一个for
语句。如果要在中为每个保留索引,则需要手动执行:-
int index = 0;
for(PeopleDetails person : people_list)
{
index++;
}
进一步讨论使用索引遍历集合的其他方法。如果您的列表人员列表
为字符串,请重试
ArrayList<String>people_list = new ArrayList<String>();
for(String i :people_list){
//Code
}
ArrayListpeople_list=new ArrayList();
用于(字符串i:人员列表){
//代码
}
它告诉您,人员列表
是一个ArrayList
而不是PeopleDetails
。您是如何声明您的ArrayList
的?请尝试发布更多相关代码,如金额方面。还有,为什么不使用普通的for循环而不是增强的for循环呢?是的,我后来意识到了这一点。因此,这个问题毫无用处。。。我怎样才能删除它???不需要删除它这个网站的目的是,问题是有其他人搜索,这可能会再次出现。好的,我会留下它。
ArrayList<String>people_list = new ArrayList<String>();
for(String i :people_list){
//Code
}