for(初始化; 布尔表达式; 更新) {
//代码语句
}
public class Test {
public static void main(String[] args){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}```
```cpp
10,20,30,40,50,
James,Larry,Tom,Lacy
/**
* ClassName: test7
* Description:
* date: 2022/7/15 13:47
*
* @author: lupeng
* @version:
* @since JDK 1.8
*/
public class test7 {
public static void main(String[] args) {
int[] a=new int[5];
String[] str=new String[]{"zhansan","lisi","wangwu"};
for (int i : a) {
System.out.println(a);
}
//a.for
for (String s : str) {
System.out.println(s);
}
/*
for(int i=0;i<str.length;i++){
String s=str[i]
}每次只是改变了指针指向
*/
System.out.println("---------------");
System.out.println(str.length);
for(int i=0;i< str.length;i++){
str[i]="sfsa";
System.out.println(str[i]);
}
}
}