美文网首页
数组的倒置

数组的倒置

作者: 哈迪斯Java | 来源:发表于2021-10-20 08:01 被阅读0次

    //规律
    //1.把arr[0]和arr[5]进行交换{66,22,33,44,55,11]}/ /2.把arr[1]和arr[4]进行交换{66,55,33,44,22,11}/ / 3.把arr[2]和arr[3]进行交换{66,55,44,33,22,11}/ /4.—共要交换3次=arr.length / 2
    //5.每次交换时,对应的下标是arr[i]和arr[arr.length - 1 -i]

    package HspLearningArray;

    public class Array07 {
    //数组反转
    public static void main(String[] args) {
    int arr1[] = {11,22,33,44,55,666};
    int temp = 0;
    int sd = arr1.length;
    for(int i = 0;i < sd/2;i++){
    temp = arr1[sd-i-1];
    arr1[sd-i-1] = arr1[i];
    arr1[i] = temp;
    }
    for (int i = 0;i < arr1.length;i++){
    System.out.print(arr1[i]+"\t");
    }
    }
    }

    结果:

    666 55 44 33 22 11

    相关文章

      网友评论

          本文标题:数组的倒置

          本文链接:https://www.haomeiwen.com/subject/tbmnoltx.html