public static void main(String[] args) {
int a[] = {1,2,3,4,5};
//遍历数组
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+=a[i];
}
System.out.println("sum = "+sum);
//查找最大元素
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i]>max){
max = a[i];
}
}
System.out.println("max = "+max);
//forEach是增强型for循环,取不到下标
for (int i : a) {
System.out.println(i);
}
System.out.println("==================");
printa(a);
System.out.println("==================");
printa(reverse(a));
}
//数组做方法入参
public static void printa(int[] mine){
for (int i = 0; i < mine.length; i++) {
System.out.print(mine[i]+" ");
}
}
//反转数组,数组做返回值
public static int[] reverse(int[] b){
int[] result = new int[b.length];
for (int i = 0,j = b.length-1; i < b.length; i++,j--) {
result[j] = b[i];
}
return result;
}