zoukankan      html  css  js  c++  java
  • 程序设计中临时变量的使用-20175218

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

    一、任务

    //定义一个数组,比如
      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();
    

    二、提交

    编译运行没有问题后,git add . git commit -m "数组元素删除,插入" git push; 提交码云上你完成的代码的链接。

    三、代码

    1. 数组插入代码
    public class insert {
        public static void Insert(int arrs[],int index,int elem) {
            for (int i = arrs.length-2; i >= index; i--) {
                arrs[i+1]=arrs[i];
            }
            arrs[index]=elem;
        }
    }
    
    1. 数组删除代码
    public class delete {
        public static void  Delete(int arrs[],int index) {
            for (int i = index; i < arrs.length-1; i++) {
                arrs[i]=arrs[i+1];
            }
            arrs[arrs.length-1]=0;
        }
    
    }
    
    1. 主代码
    public class combine {
        public static void main(String[] args) {
            int arr[] = {1,2,3,4,5,6,7,8};
            for(int i:arr){
                //System.out.println(i+" ");
                System.out.printf("%d ",i);
            }
            System.out.println();
            delete.Delete(arr,4);
            for(int i:arr){
                //System.out.println(i+" ");
                System.out.printf("%d ",i);
            }
            System.out.println();
            insert.Insert(arr,4,5);
            for(int i:arr){
                //System.out.println(i+" ");
                System.out.printf("%d ",i);
            }
            System.out.println();
        }
    
    }
    

    四、运行结果

    五、代码托管

    点击链接进入

  • 相关阅读:
    单词 统计
    第十周学习记录
    梦断代码阅读笔记03
    梦断代码阅读笔记02
    梦断代码阅读笔记01
    用户模板和用户场景
    第九周学习记录
    分享好友-分享朋友圈
    生命周期函数-页面刷新
    底部导航的设置
  • 原文地址:https://www.cnblogs.com/cjy-123/p/10746943.html
Copyright © 2011-2022 走看看