【区别】for循环中i<array.length;i<array.length-1;i<=array.length;i<=array.length-1

   The best way is to go to the editor to test it yourself

important point:

    ① The array subscript starts from 0, and the range of the array subscript is [0,arr.length-1], such as the array int[] arr = {1,2,3,4}, the length of the array, arr.length=4 , The range of the array subscript is [0,3]

    ② About the explanation in the for loop: int[] arr = {1,2,3,4}

        for(i = 0;i<array.length;i++): From the first element of the array to the last element of the array, traverse the range of elements: arr[0] to arr[3], 1, 2, 3 ,4

        for(i = 0;i<array.length-1;i++): Starting from the first element of the array, to the second element of the array, traversing the range of elements: arr[0] to arr[2], 1 ,2,3

        for(i = 0;i<=array.length;i++): The array will be out of bounds, and the subscript of the array element will not reach 4

        for(i = 0;i<=array.length-1;i++): Starting from the first element of the array, to the last element of the array, traversing the range of elements: arr[0] to arr[3], 1 ,2,3,4

  

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114826487