数组基本操作(一)

数组的基本操作:最大值,最小值,平均值,反转。

  1 package package1;
  2 
  3 /**
  4  * 数组操作
  5  * 
  6  * @author yaopan
  7  * 
  8  */
  9 public class Demo1 {
 10 
 11     public static void main(String[] args) {
 12         // TODO Auto-generated method stub
 13            double[] A={12,13,14,15,16,17,18};
 14            System.out.println("数组A中的最大值"+max(A));
 15            System.out.println("数组A中的最小值"+min(A));
 16            System.out.println("数组的平均值"+average(A));
 17            System.out.println("反转前数组A:");
 18            printfArray(A);
 19            System.out.println("
反转后数组A:");
 20            A=reverseArray(A);
 21            printfArray(A);
 22            
 23            
 24            
 25     }
 26     public static void printfArray(double[] a){
 27         for(int i=0;i<a.length;i++){
 28                
 29                System.out.print(a[i]+"	");
 30            }
 31     }
 32     //计算数组中的最大值
 33     public static double max(double[] a) {
 34         double max = a[0];
 35         for (int i = 0; i < a.length; i++) {
 36             if (a[i] >= max) {
 37 
 38                 max = a[i];
 39             }
 40         }
 41         
 42         return max;
 43     }
 44     
 45     //计算数组中的最小值
 46     public static double min(double[]  a){
 47         double min=a[0];
 48         for(int i=0;i<a.length;i++){
 49             if(a[i]<=min){min=a[i];}
 50             
 51         }
 52         
 53         return min;
 54     }
 55     
 56     //计算数组的平均值
 57     
 58     
 59     public static double average(double[] a){
 60         
 61          double sum=0;
 62          for(int i=0;i<a.length;i++){
 63              sum+=a[i];
 64          }
 65          
 66          return sum/a.length;
 67     }
 68     
 69     //复制素组
 70     
 71     public static double[] copyArray(double[] a){
 72         
 73         int n=a.length;
 74         
 75         double[] B=new double[n];
 76         
 77         for(int i=0;i<n;i++){
 78             B[i]=a[i];
 79         }
 80         
 81         return B;//返回值类型为double型数组参数。
 82         
 83     }
 84     
 85     //颠倒数组元素的顺序
 86     
 87     public static double[] reverseArray(double[] a){
 88         
 89           int n=a.length;
 90           for(int i=0;i<n/2;i++){
 91               double temp=0;
 92               temp=a[i];
 93               a[i]=a[n-i-1];
 94               a[n-i-1]=temp;
 95           }
 96           
 97           return a;
 98     }
 99     
100 }

运算结果:
  

原文地址:https://www.cnblogs.com/yaopan007/p/3704146.html