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

简介: 这篇文章对比了Java中的增强for循环(for-each循环)和传统的for循环,介绍了增强for循环的优点,如简化数组或集合的遍历、提高代码的可读性和可维护性,并指出增强for循环不适用于需要修改数组或集合元素的场景。文章还提供了增强for循环的语法格式,并展示了在实际应用中如何使用增强for循环来遍历数组和数组对象。

1、增强版和普通版对比

一般for循环

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

增强for循环

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

使用增强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(变量名);
};

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;

    }

使用增强 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 ++;
            }
        }
    }

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;
    }
}

在这里插入图片描述

相关文章
|
11天前
for循环是什么
for循环是什么
24 0
|
16天前
数组去重for循环和for循环嵌套
数组去重for循环和for循环嵌套
23 0
|
2月前
|
存储 程序员 C++
【C++小知识】基于范围的for循环(C++11)
【C++小知识】基于范围的for循环(C++11)
|
4月前
|
编译器 索引
for循环和while循环
for循环和while循环
|
5月前
|
C语言
for循环
for循环
38 1
|
5月前
|
存储 C++ 容器
【C++11】 基于范围的for循环
【C++11】 基于范围的for循环
52 0
|
11月前
|
程序员 C++
|
Python
for循环与if判断语句的运用
for循环与if判断语句的运用
104 0
|
编译器 C++ 容器
C++11之基于范围的for循环
C++11之基于范围的for循环
108 0
34 增强for循环
1 增强for介绍
73 0