“Foreach(增强for)”的版本间差异
跳到导航
跳到搜索
无编辑摘要 |
|||
第46行: | 第46行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == for、foreach、Iterator == | ||
=== 性能比较 === | |||
由下代码可以看出: | |||
# ArrayList、LinkList 中:Iterator > foreach > for; | |||
# 不要在 LinkList 中使用 for,效率太低; | |||
<syntaxhighlight lang="java" highlight=""> | |||
package com.eijux; | |||
import java.util.ArrayList; | |||
import java.util.Iterator; | |||
import java.util.LinkedList; | |||
public class TestFor { | |||
ArrayList<Integer> arrayList = new ArrayList<>(); | |||
LinkedList<Integer> linkedList = new LinkedList<>(); | |||
public TestFor() { | |||
for (int i = 0; i < 100000; i++) { | |||
arrayList.add(i); | |||
linkedList.add(i); | |||
} | |||
} | |||
public void testArrayList() { | |||
int number; | |||
long start, end, result; | |||
start = System.currentTimeMillis(); | |||
for (int i = 0; i < arrayList.size(); i++) { | |||
number = arrayList.get(i); | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("ArrayList for循环消耗时间:" + result); | |||
start = System.currentTimeMillis(); | |||
for (int i : arrayList) { | |||
number = i; | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("ArrayList foreach循环消耗时间:" + result); | |||
start = System.currentTimeMillis(); | |||
Iterator<Integer> itr = arrayList.iterator(); | |||
while (itr.hasNext()){ | |||
number = itr.next(); | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("ArrayList Iterator循环消耗时间:" + result); | |||
} | |||
public void testLinkList() { | |||
int number; | |||
long start, end, result; | |||
start = System.currentTimeMillis(); | |||
for (int i = 0; i < linkedList.size(); i++) { | |||
number = linkedList.get(i); | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("LinkList for循环消耗时间:" + result); | |||
start = System.currentTimeMillis(); | |||
for (int i : linkedList) { | |||
number = i; | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("LinkList foreach循环消耗时间:" + result); | |||
start = System.currentTimeMillis(); | |||
Iterator<Integer> itr = linkedList.iterator(); | |||
while (itr.hasNext()){ | |||
number = itr.next(); | |||
} | |||
end = System.currentTimeMillis(); | |||
result = end - start; | |||
System.out.println("LinkList Iterator循环消耗时间:" + result); | |||
} | |||
public static void main(String[] args) { | |||
(new TestFor()).testArrayList(); | |||
(new TestFor()).testLinkList(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
结果: | |||
: <syntaxhighlight lang="java" highlight=""> | |||
ArrayList for循环消耗时间:24 | |||
ArrayList foreach循环消耗时间:11 | |||
ArrayList Iterator循环消耗时间:6 | |||
LinkList for循环消耗时间:25181 | |||
LinkList foreach循环消耗时间:19 | |||
LinkList Iterator循环消耗时间:8 | |||
</syntaxhighlight> | |||
== 集合的三种遍历方式 == | == 集合的三种遍历方式 == |
2022年4月23日 (六) 17:58的版本
关于 foreach
For-Each 循环也叫增强型的 for 循环,或者叫 foreach 循环,是 JDK5.0 的新特性(其他新特性比如泛型、自动装箱等),For-Each 循环的加入简化了集合的遍历。
- foreach 可以与任何实现了 Iterable 接口的对象一起工作
语法
for(type element: array)
{
System.out.println(element);
}
使用
public class ForeachTest
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5};
System.out.println("----------普通for遍历------------");
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
System.out.println("---------增强for遍历-------------");
for(int element:arr)
{
System.out.println(element);
}
System.out.println("---------二维数组遍历-------------");
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;
for(int[] row : arr2)
{
for(int element : row)
{
System.out.println(element);
}
}
}
}
for、foreach、Iterator
性能比较
由下代码可以看出:
- ArrayList、LinkList 中:Iterator > foreach > for;
- 不要在 LinkList 中使用 for,效率太低;
package com.eijux;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
public class TestFor {
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
public TestFor() {
for (int i = 0; i < 100000; i++) {
arrayList.add(i);
linkedList.add(i);
}
}
public void testArrayList() {
int number;
long start, end, result;
start = System.currentTimeMillis();
for (int i = 0; i < arrayList.size(); i++) {
number = arrayList.get(i);
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("ArrayList for循环消耗时间:" + result);
start = System.currentTimeMillis();
for (int i : arrayList) {
number = i;
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("ArrayList foreach循环消耗时间:" + result);
start = System.currentTimeMillis();
Iterator<Integer> itr = arrayList.iterator();
while (itr.hasNext()){
number = itr.next();
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("ArrayList Iterator循环消耗时间:" + result);
}
public void testLinkList() {
int number;
long start, end, result;
start = System.currentTimeMillis();
for (int i = 0; i < linkedList.size(); i++) {
number = linkedList.get(i);
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("LinkList for循环消耗时间:" + result);
start = System.currentTimeMillis();
for (int i : linkedList) {
number = i;
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("LinkList foreach循环消耗时间:" + result);
start = System.currentTimeMillis();
Iterator<Integer> itr = linkedList.iterator();
while (itr.hasNext()){
number = itr.next();
}
end = System.currentTimeMillis();
result = end - start;
System.out.println("LinkList Iterator循环消耗时间:" + result);
}
public static void main(String[] args) {
(new TestFor()).testArrayList();
(new TestFor()).testLinkList();
}
}
结果:
ArrayList for循环消耗时间:24 ArrayList foreach循环消耗时间:11 ArrayList Iterator循环消耗时间:6 LinkList for循环消耗时间:25181 LinkList foreach循环消耗时间:19 LinkList Iterator循环消耗时间:8
集合的三种遍历方式
集合遍历的三种方式:
- 普通for
- 增强型for
- Iterator
public class ForeachTest
{
public static void main(String[] args)
{
//以三种方式遍历集合List
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("----------普通for-----------");
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
System.out.println("----------增强型for(实现了Iterable)-----------");
for(String str: list)
{
System.out.println(str);
}
System.out.println("----------Iterator-----------");
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
}
}