#For-Each 循环
#数组作方法入参
public class ArrayDome03 {
public static void main(String[] args) {
int[] array = {1,2,3,4};
//打印这个数组
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("=========");
//计算所有元素的和
int sum = 0;
for (int i = 0; i< array.length; i++){
sum = sum + array[i];
}
System.out.println(sum);
System.out.println("=========");
//查找元素中最大的值
int max = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] > max){
max = array[i];
}
}
System.out.println(max);
}
}
public class ArrayDome04 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4};
//增强for循环,1.5JDK版本才有,适合输入输出,没有下标
// for (int array : arrays) {
// System.out.println(array);
// }
//printArrays(arrays);//调用下面方法
int[] reverse = reverse(arrays);
printArrays(reverse);
?
}
//反转数组元素
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.length-1是为了取最后个数,不让数组越界
result[j] = arrays[i];
}
return result;
}
//打印数组元素
public static void printArrays(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
?
}
}
}