Java之各种for循环对比和简单实例

开心一笑

造句:
1.难过。学生:我家门前有条水沟很难过。老师批语:我更难过。
2.天真。学生:夏天真热。老师:我一头汗
3.况且。学生:一列火车经过,况且况且况且。老师:碾死我算了。

提出问题

java中的for循环比较和选择???

解决问题

例一:三种for循环:

package com.hwy.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Created by Ay on 2016/6/18.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** 第一种 **/
        List<String> firstList = new ArrayList<>();
        firstList.add("A1");
        firstList.add("A2");
        firstList.add("A3");
        for(int i=0;i<firstList.size();i++){
            System.out.println(firstList.get(i));
        }

        /** 第二种 **/
        List<String> secondList = new ArrayList<>();
        secondList.add("B1");
        secondList.add("B2");
        secondList.add("B3");
        for(String str:secondList){
            System.out.println(str);
        }

        /** 第三种 **/
        List<String> threeList = new ArrayList<>();
        threeList.add("C1");
        threeList.add("C2");
        threeList.add("C3");
        Iterator i = threeList.iterator();
        while(i.hasNext()){
            System.out.println(i.next().toString());
        }

    }

}

例二:三中for循环可能会出现的问题,对比下会发现,第一种方法代码最少,而且整洁,并且可以获得当前循环的下标值,所以个人推荐第一种,而第二种在真实项目中经常会出现没判null 造成的空指针异常。

package com.hwy.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * Created by Ay on 2016/6/18.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** 第一种 **/
        List<String> firstList = new ArrayList<>();
        firstList.add("A1");
        firstList.add("A2");
        firstList.add("A3");
        /** 设置为空,真实环境可能是查询出来为空 **/
        firstList = null;
        /** 判空处理**/
        for(int i=0;(null !=firstList) && i<firstList.size();i++){
            System.out.println(firstList.get(i));
        }

        /** 第二种 **/
        List<String> secondList = new ArrayList<>();
        secondList.add("B1");
        secondList.add("B2");
        secondList.add("B3");
        /** 设置为空 ,真实环境可能是查询出来为空**/
        secondList = null;
        /** 判空处理**/
        if(null != secondList && secondList.size()>0){
            for(String str:secondList){
                System.out.println(str);
            }
        }
        /** 第三种 **/
        List<String> threeList = new ArrayList<>();
        threeList.add("C1");
        threeList.add("C2");
        threeList.add("C3");
        /** 设置为空 **/
        threeList = null;
        /** 判空处理**/
        if(null != threeList && threeList.size() >0){
            Iterator i = threeList.iterator();
            while(i.hasNext()){
                System.out.println(i.next().toString());
            }
        }
    }

}

例三:效率问题

package com.hwy.test;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ay on 2016/6/18.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** 第一种 **/
        List<String> firstList = new ArrayList<>();
        for(int i=0;i<1000;i++){
            firstList.add("Ay" + i);
        }

        long startTime = System.currentTimeMillis();
        /** 效率低,不推荐 **/
        for(int i=0;i<firstList.size();i++){
            //System.out.println(firstList.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("the first for: " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        /** 效率高,工作中推荐 **/
        //先获得list的size,省得每次都循环获得
        int size = firstList.size();
        for(int i=0;i<size;i++){
            //System.out.println(firstList.get(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("the second for: " + (endTime - startTime));


    }

}

结果:执行时间对比,虽然执行时间差异很小,但是从代码看的话,第二种效率高

the first for: 8
......
......
the second for: 3

例四:效率问题

package com.hwy.test;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ay on 2016/6/18.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** 第一种 **/
        List<String> firstList = new ArrayList<>();
        for(int i=0;i<1000;i++){
            firstList.add("Ay" + i);
        }

        long startTime = System.currentTimeMillis();
        /** 效率稍低 **/
        int size = firstList.size();
        for(int i=0;i<size;i++){
            firstList.get(i);
            //System.out.println(firstList.get(i));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("the first for: " + (endTime - startTime));

        /** 效率高 **/
        startTime = System.currentTimeMillis();
        for(String str:firstList){
            //System.out.println(str);
        }
        endTime = System.currentTimeMillis();
        System.out.println("the enable for :" + (endTime - startTime));

    }

}

执行结果:结果表明增强for循环效率会高点

//执行第一次
the first for: 298
the enable for :108

//执行第二次
the first for: 144
the enable for :71

例五:数组和List比较

package com.hwy.test;
import java.util.Random;

/**
 * Created by Ay on 2016/6/25.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{

        String[] testData = new String[60000];
        /** 准备数据 **/
        for(int i=0;i<1000;i++){
            testData[i] = new Random(100) + "";
        }

        /** 申明变量 **/
        long startTime = 0;
        long endTime = 0;

        /** 普通for循环 **/
        startTime = System.currentTimeMillis();
        int size = testData.length;
        for(int i=0;i<size;i++){
            String str = testData[i];
        }
        endTime = System.currentTimeMillis();
        System.out.println("the normal for need time: " + (endTime - startTime));

        /** 增强for循环 **/
        startTime = System.currentTimeMillis();
        for(String temp:testData){
            String str = temp;
        }
        endTime = System.currentTimeMillis();
        System.out.println("the enbale for need time " + (endTime - startTime));

    }

}

运行第一次结果:

the normal for need time: 4
the enbale for need time 6

运行第二次结果

the normal for need time: 4
the enbale for need time 7

上面结果表明,对于数组的循环,使用普通的for循环效率更高,因为数组的索引速度更快。

感悟

读书感悟

来自《这个杀手不太冷》

  • 我认为最深沉的爱,莫过于你离开后我活成了你的样子。
  • 人生总是那么痛苦吗?还是只有小时候是这样 总是如此
  • 我已经长大,我正在变老
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,760评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 3,834评论 0 11
  • 性别:女(这还用说么?) 姓名:沐若离 个性:高冷,冷,冷,冷,底线是亲人、朋友 外表:棕色及腰长发,白嫩的皮肤吹...
    琉璃止水阅读 341评论 1 0
  • 总结下: 标题 标题要能够归纳文章主题,清晰明了,避免使用专业术语和自作聪明。 首句 首句要引人入胜。 引言 引言...
    Zerof阅读 413评论 0 6
  • 都记不清每天吃什么饭了,看来这个没愁过 今天比较清简,就几件事,日常和复习考试 最近是提前写文章早上发 想明白了些...
    良辰美LiangChen阅读 198评论 0 0