1.package finish; public class csy { public static void main(String[] args) { int arr [] ={10,20,30,40,50}; for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]); System.out.print(" "); } } }
2.package finish; public class csy { public static void main(String[] args) { int arr [] =new int[5]; arr [0] = 10; arr [1] = 20; arr [2] = 30; arr [3] = 40; arr [4] = 50; for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]); System.out.print(" "); } } }
3.package finish; public class csy { public static void main(String[] args) { int arr [] =new int[]{23,45,22,33,56}; double sum=0; for(int i = 0;i<arr.length;i++){ sum+=arr[i]; } System.out.println("最大值:"+sum); System.out.println("平均值:"+sum/arr.length); } }
4.package finish; public class csy { public static void main(String[] args) { int[] arr = { 18, 25, 7, 36, 13, 2, 89, 63 }; int max = arr[0]; int maxIndex = 0; for (int i = 1; i < arr.length; i++) { if (max <= arr[i]) { max = arr[i]; maxIndex = i; } } System.out.println("最大值为:" + max + " 最大值下标为:" + maxIndex); } }
5.package finish; public class csy { public static void main(String[] args) { int arr[] = new int[]{7,2,51,32,78,28}; int arr2[] = new int[arr.length]; for(int i = 0; i < arr.length; i++){ int value = arr[arr.length-(i+1)]; arr2[i] = value; System.out.println(arr2[i]); } } }
6.package finish; public class csy { public static void main(String[] args) { int[] arr = {5,18,28,48}; int[] arr1 = new int[5]; int a = 8; for (int i = 0; i < arr.length; i++) { if (arr[i] > a) { arr1[i] = a; for (i = i + 1; i < arr1.length; i++) { arr1[i] = arr[i - 1]; } } else { arr1[i] = arr[i]; } } for (int i = 0; i < 5; i++) { System.out.print(+arr1[i]); System.out.print(" "); } } }