Thinking in a Simple Way

搭好梯子再上来 http://simpleface.wordpress.com

java 循环语句for(int a:list)冒号的意思


例如你的数组名字为
int[] list = new int [15];  //该数组有15个元素,从0个元素开始一直到第14个结束
for(int index =0; index<15;index++)
list[index]= index*5;  //编辑一个简单的乘法公式,把所有的15个数分别乘进去;
for(int a : list)
System.out.print(a + " ");

冒号指迭代,即不断从数组的List里取其中的元素,
赋值给临时变量a,从JDK1.5开始,冒号就可以实现很方便的读取List里的内容了。
输出结果如下:

0 5 10 15 20 25 30 35 40 45 50 55 60 65 70
这15个数组的变化规律
【0】= 0 ×5 =0
【1】=1*5 = 5
【2】=2*5 = 10
 直到最后一个元素,以此类推
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: