黑马程序员技术交流社区

标题: 求解高级for循环 [打印本页]

作者: 还记得梦想吗    时间: 2013-12-13 22:58
标题: 求解高级for循环
public static ArrayList<Integer> listSort(List<Integer> la)
        {
                 Collections.sort(la);
                 for(Integer m:la)
              {
                        System.out.print(m+",");
              }
                        return null;       
        }
作者: Knife    时间: 2013-12-16 01:50
list里放的是一些Integer对象,让后对list按升序进行排序。
然后是增强for循环,取出一个Integer(m),然后输出 m+"," ,一直取到这个list没有为止.
这个程序的作用应该是对一个Integer类型的list进行排序后打印输出...
如果我回答的不好,请不要介意.我也是新手,一起学习!!
作者: 75100313    时间: 2013-12-16 21:38
增强for循环  格式
for(类型  变量:要遍历的集合){
//每次遍历一个 放到变量里面
           System.out.print(变量+",");

我也是新人 我就说个大概的意思吧
作者: 郭涛    时间: 2013-12-16 21:56
forearch 增强for循环
格式:
for(类型 变量名:容器){
     循环体
}
容器内有多少个元素就执行多少次循环体,每次循环变量只想容器中不同的元素。
给个小例子:
  1. public class Test001 {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                 String[] str = {"a","b","c","d","e"};
  5.                 for (String string : str) {
  6.                         System.out.println(string);
  7.                 }
  8.         }

  9. }
复制代码

作者: 还记得梦想吗    时间: 2013-12-16 23:28
郭涛 发表于 2013-12-16 21:56
forearch 增强for循环
格式:
for(类型 变量名:容器){

:).........................收益了
作者: 想你的夜    时间: 2013-12-17 20:05
不是什么高级for循环,只是jdk1.5的新特性,新加的for循环增强,是for循环中的特例,用于集合和数组,在集合中使用可以提高编码效率, 优点主要体现在集合中,但是for循环增强并不能完全取代传统的for循环。比较

//传统的数组遍历
                String[] persons={"李华","王明","张亮"};
                for(int i=0;i<persons.length;i++){
                        System.out.println(persons[i]);
                }
//使用增强for循环的数组遍历
                String[] persons={"李华","王明","张亮"};
                for(String person:persons){
                        System.out.println(person);




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2