Java??foreach??ù??????????????
???????????? ???????[ 2016/12/13 11:18:30 ] ??????????????????? Java
????????foreach??????????????????????????????????????“ foreach??ù?????????????????????for i++?????????? ”?????????????????????????????JVM??foreach???????????????????????????????foreach????????????????????????????????????? Java ?????????????? ?????????2?????“???????iterator()”??????????????!
??????????1000*1000????????
????ArrayLIst??????????? ????????foreach???for++??????Щ??????????? ??
long start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i ++) {
String s = list.get(i);
}
System.out.println("arrayList for i++ on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (String s : list) {
//
}
System.out.println("arrayList foreach on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
??????н????
??????1?Σ?
????arrayList for i++ on 1000*1000 records time waste: 0
????arrayList foreach on 1000*1000 records time waste: 16
??????2??:
????arrayList for i++ on 1000*1000 records time waste: 0
????arrayList foreach on 1000*1000 records time waste: 0
??????3??:
????arrayList for i++ on 1000*1000 records time waste: 0
????arrayList foreach on 1000*1000 records time waste: 0
????LinkedLIst??????????? ????????foreach???for++??N????????????N?????????????????? ??
for (int i = 0; i < list.size(); i ++) {
String s = list.get(i);
}
System.out.println("LinkedList for i++ on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (String s : list) {
//
}
System.out.println("LinkedList foreach on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
?????????
????????Σ?
????LinkedList for i++ on 1000*1000 records time waste: 41224
????LinkedList foreach on 1000*1000 records time waste: 11
?????????:
????LinkedList for i++ on 1000*1000 records time waste: 43914
????LinkedList foreach on 1000*1000 records time waste: 11
?????????Σ?
????LinkedList for i++ on 1000*1000 records time waste: 57320
????LinkedList foreach on 1000*1000 records time waste: 16
????Array??????????? ????????foreach???for++??????Щ???????????????ArrayList?????????? ??
String[] array = (String[])list.toArray(new String[]{});
long start = System.currentTimeMillis();
for (int i = 0; i < array.length; i ++) {
String s = array[i];
}
System.out.println("Array for i++ on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (String s : array) {
//
}
System.out.println("Array foreach on 1000*1000 records time waste: " + (System.currentTimeMillis() - start));
?????????
????????Σ?
????Array for i++ on 1000*1000 records time waste: 0
????Array foreach on 1000*1000 records time waste: 0
????????Σ?
????Array for i++ on 1000*1000 records time waste: 0
????Array foreach on 1000*1000 records time waste: 16
?????????Σ?
????Array for i++ on 1000*1000 records time waste: 0
????Array foreach on 1000*1000 records time waste: 0
????foreach??for++????棿
???????????λ??????????
????every time you run into this loop?? if strings is an Iterable?? you will create a new Iterator instance. If you’re using an ArrayList?? this is going to be allocating an object with 3 ints on your heap?????????
??????????????????????????????” with 3 ints on your heap “??????????????foreach?????????JVM??????????????????????????????????????????????????????????????????java.util.ArrayList?????
public Iterator<E> iterator() {
return new Itr(); ////???????for++???
}
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification(); //???????for++???
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
?????????????foreach??for++????new??????????????????????????????????ж???????????for++????μ????????????????????????????
???????
????1??foreach???????????????for++????????????????
????2??foreach???????????????for++???????????????????????
????3??foreach??????????????for++???????????????????????????????????
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11