标签:method package public 最大值 平均值
package com.ctu.java.exer; public class TestArray { public static void main(String[] args) { int[] arr = new int[]{12,4,76,0,-98,-54,4,100}; ArrayUtil au = new ArrayUtil(); int max = au.getMax(arr); System.out.println("最大值为:" + max); int avg = au.avg(arr); System.out.println("平均值为:" + avg); au.printArray(arr); System.out.println("反转数组:" ); au.reverse(arr); au.printArray(arr); System.out.println("对数组排序:"); au.sort(arr,"asc"); au.printArray(arr); // TODO Auto-generated method stub } package com.ctu.java.exer; public class ArrayUtil { public int getMax(int[] arr){ int max = arr[0]; for(int i = 1; i < arr.length;i++){ if(max < arr[i]){ max = arr[i]; } } return max; } public int getMin(int[] arr){ int min = arr[0]; for(int i = 1; i < arr.length;i++){ if(min > arr[i]){ min = arr[i]; } } return min ; } public void printArray(int[] arr){ System.out.print("["); for(int i = 0; i < arr.length;i++){ System.out.print(arr[i]+ "\t"); } System.out.println("]"); } public int avg(int[] arr){ int sum = getSum(arr); return sum/arr.length; } public int getSum(int[] arr){ int sum = 0; for(int i =0 ; i < arr.length;i++){ sum +=arr[i]; } return sum; } public int[] reverse(int[] arr){ for(int x = 0,y = arr.length -1; x<y;x++,y--){ int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } return arr ; } public int[] copy(int[] arr){ int[] arr1 = new int [arr.length]; for(int i = 0; i < arr.length ;i++){ arr1[i] = arr[i]; } return arr1; } public void sort(int[] arr,String desc){ if(desc == "asc"){ for(int i = 0; i < arr.length-1;i++){ for(int j = 0;j < arr.length - 1 -i;j++){ if(arr[j] > arr[j + 1]){ swap(arr,j,j+1); } } } }else if(desc == "desc"){ for(int i = 0; i < arr.length-1;i++){ for(int j = 0;j < arr.length - 1 -i;j++){ if(arr[j] < arr[j + 1]){ swap(arr,j,j+1); } } } }else{ System.out.println("你输入的方式有误!"); } } public void swap(int[] arr,int i ,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
}
标签:method package public 最大值 平均值
原文地址:http://9815936.blog.51cto.com/9805936/1601678