数组的一些运用
public class Demo3 { public static void main(String[] args) { int[] a = {1, 2, 23, 4, 9}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } int sum = 0; for (int i = 0;i<a.length;i++){ sum = sum + a[i]; } System.out.println("数值的和为:"+sum); int max = a[0]; for (int i = 0;i<a.length;i++){ if (a[i]>max){ max=a[i]; } } System.out.println("最大值为:"+max); } }
数组取反
public class Demo4 { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; //没有下标 /*for (int i : arrays) { System.out.println(i); }*/ int[] reverse = reverse(arrays); printArray(reverse); //printArray(arrays); } public static void printArray(int[] arrays){ for (int i = 0;i < arrays.length;i++){ System.out.print(arrays[i]+" "); } } public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; //反转操作 for (int i = 0, j=result.length-1;i < arrays.length;i++,j--){ //result[]= arrays[i]; result[j] = arrays[i]; } return result; } }