【发布时间】:2014-01-24 13:04:15
【问题描述】:
我正在使用 for each 循环迭代 ArrayList,但我不知道如何获取循环所在的当前索引。
我用谷歌搜索过,但找不到任何有用的东西。
如果有人能告诉我如何获取当前索引,我将不胜感激。
【问题讨论】:
标签: java arraylist foreach indexing
我正在使用 for each 循环迭代 ArrayList,但我不知道如何获取循环所在的当前索引。
我用谷歌搜索过,但找不到任何有用的东西。
如果有人能告诉我如何获取当前索引,我将不胜感激。
【问题讨论】:
标签: java arraylist foreach indexing
只需使用传统的 for 循环:
for (int i = 0; i < yourArrayList.size(); i ++) {
// i is the index
// yourArrayList.get(i) is the element
}
【讨论】:
窃取门把手的答案,但做出改变,否则会让我像海盗一样发疯:
int arraySize = yourArrayList.size();
for (int i = 0; i < arraySize; i ++) {
// i is the index
// yourArrayList.get(i) is the element
}
或者
int currentPosition = 0;
for (myItemType myItem :myArrayList) {
// do stuff
currentPosition++;
}
【讨论】:
arraySize=yourArrayList.size() 或根据需要使用++ 或--。
for( int i =0, n = list.size(); i<n; i++ ) 表示善意。
如果它在容器中,您可以使用list.indexOf(objectYouWantToFind); 获取其索引,否则您将收到-1。
【讨论】:
indexOf 的一个问题是性能问题。 indexOf 每次调用它时都会对所有元素进行线性扫描,而 @Doorknob 使用显式 for 循环的回答避免了额外的扫描。例如,如果您正在遍历一个循环并每次调用 indexOf,那么您有 O(n^2)。如果您将get 视为 O(1),门把手的答案是 O(n)。您可能会对此链接感兴趣:java-performance.info/arraylist-performance
使用传统循环。 Java 中的 For-each 语句不承诺对集合进行排序。真正知道索引的唯一方法是在每次迭代时查询列表。
for(int i=0; i<list.size; i++){
// you have your index, i
// the item is at list.get(i)
}
依靠 iterator() 实现,您还可以使用替代方案(在 cmets 中 +1 Zak):
int position = 0;
for(Item i : list){
// do stuff with `i`
// increase position as the last action in the loop
position++;
}
来自文档:Iterator<E> iterator() Returns an iterator over the elements in this list in proper sequence
这就是为什么我通常不使用for的简写形式:
import java.util.ArrayList;
import java.util.Iterator;
public class MyList<T> extends ArrayList<T> {
// the main function to run the example
public static void main(String[] args){
// make a list of my special type
MyList<Integer> list = new MyList<Integer>();
// add 10 items to it
for (int i =0; i<10; i++){
list.add(i);
}
// print the list using the for-each mechanism (it's actually an iterator...)
for (Integer i : list){
System.out.println(i);
}
}
// I like lists that start at 3!
// make my list return an iterator in the middle of the list...
@Override
public Iterator<T> iterator(){
return this.listIterator(3);
}
}
显然,您希望在第一次迭代中拥有第一项,但显然情况并非如此,因为迭代器可以通过多种方式实现,而 Java 的 foreach 取决于底层的 iterator 实现。
另外,您必须在循环结束时使用position++,这可能容易出错(不知道,通常不使用它..)。
也就是说,它确实提高了可读性,就像这个 stackoverflow question 关于 Java 的 foreach 中提到的那样。
如需了解更多信息,请参阅How does the Java for each loop work?。
【讨论】:
如果是迭代一个List,可以使用indexOf()方法,例子:
List<Animal> animals = new ArrayList<Animal>();
Animal a = new Animal();
a.setAnimalCode(1);
animals.add(a);
a = new Animal();
a.setAnimalCode(35);
animals.add(a);
a = new Animal();
a.setAnimalCode(12);
animals.add(a);
for(Animal a: animals)
if(animal.getAnimalCode()==35)
System.out.println("Index of chosen one: "+animals.indexOf(a));
选择的索引:2
【讨论】:
有点骇人听闻,但你肯定可以在 for each 循环中有一个索引:
public class Counter{
private Integer i;
public Counter(Integer i) { this.i = i; }
public void incrementOne(){ this.i = getI() + 1; }
//getter and setter for i
}
而且,在你的 for each 循环中的其他地方:
外星人.java
Counter counter = new Counter(0);
List<String> list = new ArrayList<>();
//have some values in the list
list.forEach(i-> {
//i will have the value and the counter will have the count!
System.out.println(counter.getI());
counter.incrementOne();
});
否则,您总是有一个 list.indexOf(i),其中 i 是列表中的每个对象。 :P
【讨论】: