2023.7.16
1 import java.sql.SQLOutput; 2 import java.util.Scanner; 3 //数组的使用 4 public class test { 5 public static void main(String[] args) 6 { 7 int[] arrays = {1,2,3,4,5}; 8 //for_each循环 9 for (int array : arrays) {//使用时可以写为arrays.for回车 10 System.out.print(array); 11 } 12 System.out.println("//for_each循环==========="); 13 //可以作为参数 14 15 System.out.print("max = "+max(arrays)); 16 System.out.println(" //可以作为参数==========="); 17 18 //可以作为返回值类型 19 for (int i : reverse(arrays)) { 20 System.out.print(i); 21 } 22 System.out.println("//可以作为返回值类型==========="); 23 } 24 public static int max(int[] arrays){ 25 int max = arrays[0]; 26 for (int i = 1; i < arrays.length; i++) { 27 if(arrays[i]>max){ 28 max = arrays[i]; 29 } 30 } 31 return max; 32 } 33 //反转数组 34 public static int[] reverse(int[] arrays){ 35 int[] ret = new int[arrays.length]; 36 int n = arrays.length-1; 37 for (int i = 0; i < arrays.length; i++) { 38 ret[i] = arrays[n]; 39 n--; 40 } 41 return ret; 42 } 43 }