一维数组的增加,删除,找最大数

1,数组的增加   

 1 package shuzu;
 2 
 3 public class Shuzu {
 4 
 5  public int[] insertOneNum(int num[], int pos, int nums) {
 6        int[] numE = new int[num.length + 1]; 
 7             for (int i = 0; i < pos; i++) {
 8            numE[i] = num[i]; 
 9                   } 
10  
11            numE[pos] = nums; 
12 
13            for(int i = pos; i <num.length; i++) { 
14                   numE[i + 1] = num[i];
15             
16           } 
17            return numE; 
18      }
19  
public static void main(String[] args) { 20 int[] num = new int[] { 2, 4, 5, 1, 6, 0 }; 21 22 Shuzu sz = new Shuzu();// 声明一个对象 23 num=sz.insertOneNum(num,3,3); 24 for(int i=0;i<num.length;i++){ 25 System.out.print(num[i]+" "); 26 } 27 } 28 }

执行是结果:2, 4, 5,3,1, 6,

2,删除一个数

 1 package shuzu;
 2 
 3 public class Shuzu {
 4      public void deleOneNum(int[] num, int pos) {
 5         for (int i = pos; i < num.length; i++) {
 6             num[i - 1] = num[i];
 7         }
 8 
 9     }
10     public static void main(String[] args) {
11      int[] num = new int[] { 2, 4, 5, 1, 6, 0 }; 
12              Shuzu dz=new Shuzu();
13               dz.deleOneNum(num,1); 
14              for(int i=0;i<num.length;i++){ 
15              System.out.print(num[i]+" ");
16            }
17        }
18 }

执行的结果是:4 5 1 6 0 0

3.找最大的数

  

 1 package shuzu;
 2 
 3 public class Shuzu {
 4    public int findOneNum(int[] num) {
 5         int Max = num[0];
 6         for (int i = 0; i < num.length; i++) {
 7             if (Max < num[i]) {
 8                 Max = num[i];
 9             }
10         }
11         return Max;
12     }
13  
14    public static void main(String[] args) {
15         int[] num = new int[] { 2, 4, 5, 1, 6, 0 };
16                
17                 Shuzu fO = new Shuzu();
18         int Max = 0;
19         Max = fO.findOneNum(num);
20         System.out.print("最大数是=" + Max);
21           }
22   }

执行的结果是:最大数是=6

4,找出数组中不同的数

   

 1 package shuzu;
 2 
 3 public class Shuzu {
 4    public void findNuml(int[] nums){
 5         int j;
 6         int[] temp=new int[nums.length];
 7         
 8         for(int i=0;i<nums.length;i++){
 9             
10             for( j=0;j<i;j++) {
11                
12                 if(nums[j]==nums[i]) 
13                 
14                     break;
15                }
16            
17             
18             if(i==j){
19                 
20                 System.out.print(nums[i]+" "); 
21                }
22             }
23          
24     }
25       
26 
27   public static void main(String[] args) {
28        int[] nums=new int[] {1,1,3,4,1,4,5,3,1,65,3};
29     Shuzu Fz=new Shuzu();
30     Fz.findNuml(nums);
31         }
32   }

输出的结果:1 3 4 5 65

原文地址:https://www.cnblogs.com/sunli0205/p/5219775.html