A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 还记得梦想吗 中级黑马   /  2013-12-13 22:58  /  1101 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public static ArrayList<Integer> listSort(List<Integer> la)
        {
                 Collections.sort(la);
                 for(Integer m:la)
              {
                        System.out.print(m+",");
              }
                        return null;       
        }

6 个回复

正序浏览
不是什么高级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);
回复 使用道具 举报
郭涛 发表于 2013-12-16 21:56
forearch 增强for循环
格式:
for(类型 变量名:容器){

:).........................收益了
回复 使用道具 举报
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. }
复制代码
回复 使用道具 举报
增强for循环  格式
for(类型  变量:要遍历的集合){
//每次遍历一个 放到变量里面
           System.out.print(变量+",");

我也是新人 我就说个大概的意思吧
回复 使用道具 举报
list里放的是一些Integer对象,让后对list按升序进行排序。
然后是增强for循环,取出一个Integer(m),然后输出 m+"," ,一直取到这个list没有为止.
这个程序的作用应该是对一个Integer类型的list进行排序后打印输出...
如果我回答的不好,请不要介意.我也是新手,一起学习!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马