看java核心技术里一个例子
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/18 07:48:02
看java核心技术里一个例子
public class welcome
{
public static void main(String[] args)
{
String[] greeting=new String[3];
greeting[0]="welcome to core java";
greeting[1]="by Gay Horstmann";
greeting[2]="and Gary Cornell";
for(String g:greeting)
System.out.println(g);
}
}
代码无误,执行也正常.可是for(String g:greeting)这一句我看不懂,这是for循环么.
public class welcome
{
public static void main(String[] args)
{
String[] greeting=new String[3];
greeting[0]="welcome to core java";
greeting[1]="by Gay Horstmann";
greeting[2]="and Gary Cornell";
for(String g:greeting)
System.out.println(g);
}
}
代码无误,执行也正常.可是for(String g:greeting)这一句我看不懂,这是for循环么.
for each语法.
for(String g:greeting)
System.out.println(g); 相当于
for(int i = 0; i < greeting.length; i ++ ) {
String g = greeting[i];
System.out.println(g);
}
for(String g:greeting)
System.out.println(g); 相当于
for(int i = 0; i < greeting.length; i ++ ) {
String g = greeting[i];
System.out.println(g);
}