package com.ctgu.java.exer; public class TestArray3 { public static void main(String[] args){ int[] arr = new int[]{12,43,9,0,-65,-99,100,9}; int max = arr[0]; for(int i = 1; i < arr.length;i++ ){ if(max < arr[i]){ max = arr[i]; } } System.out.println("数组最大值为:" + max); int min=arr[0]; for(int i = 1;i< arr.length;i++){ if(min >arr[i]){ min = arr[i]; } } System.out.println("数组最小值为:" + min); int sum = 0; for(int i = 0; i < arr.length; i++){ sum +=arr[i]; } System.out.println("总和为:" +sum); int avg = 0; avg = sum / arr.length; System.out.println("平均数为:" +avg ); // 数组元素复制 int [] arr1 = new int[arr.length]; for(int i = 0 ; i < arr1.length;i++){ arr1[i] = arr[i]; } //数组元素的反转 //for(int i = 0; i< arr.length/2 ; i++){ //int temp =arr[i]; //arr[i] = arr[arr.length-1 - i]; //arr[arr.length-1-i]=temp; //} //反转2 for(int x = 0 , y = arr.length - 1; x < y;x++,y--){ int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp ; } for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + "\t"); } //冒泡排序 从小到大 // 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]){ // int temp = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = temp ; // } // } // } //直接选择排序 从小大的 for(int i = 0; i < arr.length;i++){ int t = i; for(int j =i ;j < arr.length - 1;j++){ if(arr[t]>arr[j]){ t=j; } } if(t != i){ int temp = arr[t]; arr[t] = arr[i]; arr[i] = temp; } } System.out.println("排序以后:"); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + "\t"); } } }
时间: 2024-10-08 09:47:07