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

一、题目要求

任务:
  //定义一个数组,比如
  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 Temp {
    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;
       
        //打印出 1 2 3 4 6 7 8 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();
    }

}
```

问题解析:

在这次实验中我不了解关于删除添加的代码该如何填写在参考了如下的网上的代码之后我学会了删除的方法。
```
// 答案是太能了:不用集合一样办到:(ArryList集合底层就是这么弄的)
// 方法很多种,软copy和硬copy,还有字符串桥接,随便举两种吧
public class ArrayTest {
    public static void main(String[] args) {
        int a1[]={1,3,4,5,0,0,9,6,0,5,4,7,6,7,0,5};
        int a2[] = { 1, 0, 5,0,6,0,4, 1, 0 };
        
        //测试第一种办法:
        System.out.println("原数组:");
        for (int n : a1)
            System.out.print(n + ",");
        // 删除元素!
        a1=volume(a1,0);
        System.out.println("\n现数组:");
        for (int n : a1)
            System.out.print(n + ",");
        
        System.out.println("\n\n-----------------分割线--------------------\n");
        
        //测试第二种办法:
        System.out.println("原数组:");
        for (int n : a2)
            System.out.print(n + ",");
        // 删除元素!
        a2 = value(a2, 0);
        System.out.println("\n现数组:");
        for (int n : a2)
            System.out.print(n + ",");
        
    }// 方法1,浅复制!
    private static int[] volume(int[] arr, int key) {
        int count = 0;
        for (int i = 0, j = arr.length - 1; i <= j; i++, j--) {
            if (arr[i] == key && i != j)
                count++;
            if (arr[j] == key && i != j)
                count++;
            if (arr[i] == key && i == j)
                count++;
        }
        int nrr[] = new int[arr.length - count];
        count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == key)
                continue;
            else
                nrr[count++] = arr[i];
        }
        return arr = nrr;
    }//方法2:这方法最简单,的搞法,用个字符串桥接一下即可!
    private static int[] value(int[] arr, int key) {
        StringBuilder str=new StringBuilder();
        for (int i = 0; i < arr.length; i++)
            if(arr[i]!=key)
                str.append(arr[i]);
        char[] chs=new String(str).toCharArray();
        int[] orr=new int[chs.length];
        for (int i = 0; i < orr.length; i++) {
            orr[i]=chs[i]-'0';
        }
        return arr=orr;
```
在参考了如下的代码之后可以学会数字的插入
```
/**
给数组指定位置数组的插入
*/
import java.util.*;
public class ArrayInsert{
    public static void main(String []args){
        System.out.println("请用键盘输入5个数:");
        int [] array =new int[10];
        Scanner sc=new Scanner(System.in);
        //通过键盘向数组输入数
        for(int i=0;i<array.length-5;i++){
            array[i]=sc.nextInt();
        }
        //遍历数组
        System.out.print("原数组为:");
        for(int a:array){
            System.out.print("  "+a);
        }
        //向指定的位置插入数
        System.out.println("\n请输入插入位置:有效位置为0-----"+(array.length-1));
        int index=sc.nextInt();
        System.out.println("\n请输入插入的值-----");
        int num=sc.nextInt();

        //调用静态函数index
        //遍历插入后的数组
            System.out.println("插入元素之后的数组遍历:");
            Insert(index,num,array);
             for(int i=0;i<array.length;i++){
            System.out.print("  "+array[i]);
        }
    }
    //向数组指定位置插入数据方法
    public static int[] Insert(int index,int num,int a[]){
            //如果有元素,在索引之后的元素向后移一位,
            for(int   a[i]=a[i-1];
            }
            a[index]=num;
 return a;     
    }
}

```
 

猜你喜欢

转载自www.cnblogs.com/waaaaa/p/10747610.html