A-A+

Java语言list遍历的三种方法

2017年01月27日 Java, 编程随笔 暂无评论 阅读 5 views 次

Java语言list遍历的三种方法,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.hisense.tiger.list;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class ListTest {
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
long t1,t2;
for(int j = 0; j < 10000000; j++)
{
list.add("aaaaaa" + j);
}
System.out.println("List first visit method:");
t1=System.currentTimeMillis();
for(String tmp:list)
{
//System.out.println(tmp);
}
t2=System.currentTimeMillis();
System.out.println("Run Time:" + (t2 -t1) + "(ms)");
System.out.println("List second visit method:");
 
t1=System.currentTimeMillis();
for(int i = 0; i < list.size(); i++)
{
list.get(i);
//System.out.println(list.get(i));
}
t2=System.currentTimeMillis();
System.out.println("Run Time:" + (t2 -t1) + "(ms)");
 
System.out.println("List Third visit method:");
Iterator<String> iter = list.iterator();
 
t1=System.currentTimeMillis();
while(iter.hasNext())
{
iter.next();
//System.out.println(iter.next());
}
t2=System.currentTimeMillis();
System.out.println("Run Time:" + (t2 -t1) + "(ms)");
 
System.out.println("Finished!!!!!!!!");
}
}

标签:

给我留言