• 程序设计中临时变量的使用 20175204


    程序设计中临时变量的使用 20175204

    1.作业要求

    提交:
    编译运行没有问题后,git add . git commit -m "数组元素删除,插入" git push; 提交码云上你完成的代码的链接。
    任务:
    //定义一个数组,比如
    int arr[] = {1,2,3,4,5,6,7,8};

    //打印原始数组的值
    for(int i:arr){
    System.out.print(i + " ");
    }
    System.out.println();

    // 添加代码删除上面数组中的5
    ...

    //打印出 1 2 3 4 6 7 8 0
    for(int i:arr){
    System.out.print(i + " ");
    }
    System.out.println();

    // 添加代码再在4后面5
    ...

    //打印出 1 2 3 4 5 6 7 8
    for(int i:arr){
    System.out.print(i + " ");
    }
    System.out.println();


    2.实验代码:

    /*任务:
      //定义一个数组,比如
      int arr[] = {1,2,3,4,5,6,7,8};
    
      //打印原始数组的值
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();
    
      // 添加代码删除上面数组中的5
      ...
    
      //打印出 1 2 3 4 6 7 8 0
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();
    
      // 添加代码再在4后面5
      ...
    
      //打印出 1 2 3 4 5 6 7 8
      for(int i:arr){
          System.out.print(i + " ");
      }
      System.out.println();*/
    
    public class kexiaceshi {
        public static void main(String[] args) {
            int arr[] = {1,2,3,4,5,6,7,8};
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
            // 添加代码删除上面数组中的5
            int temp = -1;
            for(int i:arr){
                if(arr[i] == 5){
                    temp = i;
                    break;
                }
            }
            for(int i=temp+1;i<arr.length;i++){            arr[i-1] = arr[i];
            }
            arr[arr.length-1] = 0;     //     //从数组中删除一个元素,并后面元素前移: 首先找到该元素所在的下标,将该下标之后的所有元素前移,将末尾元素赋值为0
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
            // 添加代码再在4后面5
            for(int i:arr){
                if(arr[i] == 4){
                    temp = i;
                    break;
                }
            }
            for(int i=arr.length-1;i>temp+1;i--){
                arr[i] = arr[i-1];
            }
            arr[temp+1] = 5;     //从数组中添加一个元素,并后面元素后移: 首先找到该元素之后的所在的下标,将该下标之后的所有元素后移移,将找到元素赋值为设定值。
            //打印出 1 2 3 4 5 6 7 8
            for(int i:arr){
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
    

    3.运行结果截图

  • 相关阅读:
    Steps to Writing Well----Reading Notes
    How to Improve Reading Skills
    Requirement-Driven Linux Shell Programming
    Linux tar command usage
    MVC和MVVM模型
    js中特殊的宏任务
    js 超浓缩 双向绑定
    JavaScript 中的遍历详解
    多段动画整合为一个动画的思路
    Js事件循环(Event Loop)机制
  • 原文地址:https://www.cnblogs.com/zyzgl/p/10745255.html
Copyright © 2020-2023  润新知