public class Array3 {
public static void main(String[] args) {
int[] myArray = {1,2,3,4,5,6,7,8};
//打印数组所有元素
System.out.print("数组里面的元素有:");
for (int i = 0; i < myArray.length ; i++) {
System.out.print(myArray[i]+" ");
}
System.out.println(" ");
//计算数组所有元素的和
int sum = 0;
for (int i = 0; i < myArray.length; i++) {
sum+=myArray[i];
}
System.out.println("数组元素之和为:"+sum);
printArray(reverse(myArray));
}
//打印int数组
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
//反转int数组
public static int[] reverse(int[] arrays){
int tailIndex = arrays.length-1;
int temp = 0;
for (int i = 0; i < arrays.length/2; i++) {
temp = arrays[i];
arrays[i] = arrays[tailIndex];
arrays[tailIndex] = temp;
tailIndex--;
}
return arrays;
}
}