foreach基本语法:
在Java中:
先定义数组,
for(数据类型 变量名 : 数组名){
System.out.print(变量名);
}
1 public class foreach { 2 3 public static void main(String[] args) { 4 String[] str = {"1","a","2","b","3","c","4","d","5","e","6","f"}; 5 6 for(String strs : str) { 7 System.out.print(strs + " "); 8 } 9 10 } 11 12 }
运行结果:
JavaScript中foreach的语法:
for(数据类型 变量名 in 数组名){
document.write(数组名[变量名]);
}
1 <script> 2 var i = new Array("abc","def","ghi"); 3 4 for(var a in i){ 5 document.write(i[a] + " "); 6 } 7 8 </script>
运行结果:
c#中foreach的语法:
foreach(数据类型 变量名 in 数组名){
}
因电脑原因,所以c#的代码与运行结果无法展示