• 增强for循环和一般for循环的对比使用


    文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。个人B站主页热爱技术的小郑 ,视频内容主要是对应文章的视频讲解形式。有兴趣的可以关注一下。为何分享? 踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。

                                                                                  热爱技术的小郑

    1、增强版和普通版对比

    一般for循环

        int[] num = {1,2,3,4,5,6};
        for(int i =  0 ; i<num.length ; i++){ 
            System.out.println("元素:"+ num[i]); 
        } 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    增强for循环

    	 int[] num = {1,2,3,4,5,6};
         for(int i :num){   //集合或数组a : 数组名称num
             System.out.println("元素:"+ i); 
         } 
    
    • 1
    • 2
    • 3
    • 4

    使用增强for循环的优点:

    • 增强 for 循环的作用是简化集合或数组的遍历操作,减少编写代码的工作量 增加代码的可读性和可维护性。使用增强 for 循环可以避免使用传统的 for 循环的索引变量,使代码更简洁、清爽,并且可以 避免数组越界等常见问题。

    • 增强 for 循环最大的好处是在语法层面上提供了一种 更简单、更直观的集合遍历方式 ,使得代码变得更加简单、易读、易写,从而提高了编码效率和代码可维护性。

    • 需要注意的是,增强 for 循环只能用于遍历集合或数组中的元素, 无法用于修改集合或数组中的元素。如果需要修改集合或数组中的元素,仍然需要使用传统的 for 循环或迭代器等方法。

    2、什么是增强for循环?

    增强for循环 (也称for each循环) 是迭代器遍历方法的一个“简化版”,是JDK1.5以后出来的一个高级for循环 专门用来遍历数组和集合。

    其内部原理是一个Iteration迭代器,在遍历数组/集合的过程中,不能对集合中的元素进行增删操作。

    3、增强for循环的使用(语法)

    1.使用范围:用来遍历集合和数组(必须有遍历目标,目标只能是集合或者数组),所有单列表集合都可以使用增强for循环。
      2.格式如下:

    for(ElementType element: arrayName) 
    { //集合或数组的数据类型 变量名:集合名/数组名
    	System.out.println(变量名)};
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、实际应用

    4.1 遍历数组

    普通的一般 for 使用

        /**
         * 力扣  移除数组中的指定元素
         * @param nums
         * @param val
         * @return
         */
        public static int removeElement(int[] nums, int val) {
            int [] mynums = new int[nums.length];
            int rs = 0;
            for(int i = 0; i < nums.length; i++){
                if(nums[i] != val){
                    mynums[rs] = nums[i];
                    rs ++;
                }
            }
            return mynums.length;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    使用增强 for 循环

        /**
         * 增强for循环
         * @param nums
         * @param val
         */
        public static void test(int [] nums,int val){
            int rs = 0;
            for(int num : nums){
                if(num != val){
                    nums[rs] = num;
                    rs ++;
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4.2 遍历数组对象

    遍历数组对象,通过对象访问对象的属性值。

        /**
         * 对象循环
         * @param students
         */
        public static void testObject(Student [] students){
            for(Student student : students){
                System.out.println("姓名:" + student.name + ",年龄:" + student.age);
            }
        }
    
    
        public static void main(String[] args) {
            Student  student1 = new Student("张三1",18);
            Student  student2 = new Student("张三2",19);
            Student  student3 = new Student("张三3",20);
            Student  student4 = new Student("张三4",21);
            //组合数组对象
            Student [] students = {student1, student2, student3, student4};
    
            Solution.testObject(students);
    
        }
    
    
    //学生类
    class Student{
        String name;
        int age;
    
        Student(){
    
        }
    
        Student(String name, int age){
            this.name = name;
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    在这里插入图片描述

  • 相关阅读:
    Vitis_米联客开发板MZU07_7EG上手_1
    Java概述与基础
    ROS2 中的轻量级、自动化、受控回放
    俄罗斯方块c语言
    关于LDPC编译码参数如何选择确定
    如何在springboot项目中引入knife4j接口文档
    【微信小程序】button和image组件的基本使用
    华为ModelArts自定义镜像(PyTorch镜像)
    字符串匹配算法KMP原理
    robots.txt限制搜索引擎收录
  • 原文地址:https://blog.csdn.net/weixin_43304253/article/details/133309610