android中for循环的简化方式“:”的应用
(2011-08-23 10:24:53)
标签:
it |
分类: android学习 |
String a;
File[] files=new File("/").listFiles();
for( File f : files )
{
a += f.getPath() + "\n" ;
……
}
这是JDK 1.5还是1.6中引进的语言新特性。简化了循环的书写。
和整个基本相同作用,
for(int i=0;i<files.length;i++)
{
File f = files[i];
//其他操作
}
类似的对于其他数组比如
int[] a = {1, 2, 3, 4, 5, 6};
也可以用
for(int i: a) {
System.out.print(i+"\t");
}
来打印a数组中所有元素。