struts中的一条for语句,看不懂,请高手指教。

来源:百度知道 编辑:UC知道 时间:2024/07/07 00:30:48
String[] forwardnames = mapping.findForwards();
for(String forwardname:forwardnames){
Actionforward forward = mapping.findforward(forwardname);
String path = fordward..getpath();

}

":"前边是一个数据类型的变量,后边是一个集合(包含数组、list、map等都可以元素类型跟前边的数据类型一样就行了)。
例如:int[] a = int[10]; ----> for(int i:a)(i为a中的一个元素)
List<Integer> l = new ArrayList<Integer>();---->for(Integer i:l)(i为l中的一个元素)
Map<String,Integer> m=new HashMap<String,Integer>()
for(String s:m.keySet())(s为m中的一个key值)
for(Integer i:m.values())(i为m中的一个value值)...
这是JDK1.5的新特性挺好用的。

这个是jdk1.5的语法,方便遍历 在jdk1.4就是这样的

for(int i=0;i<forwardnames.length;i++){
String forwardname=forwardnames[i];
Actionforward forward = mapping.findforward(forwardname);
String path = fordward.getpath();

}
一样的效果的,但是用foreah语句更方便。。