数组反转
数组反转:
public class ArrayDemo { public static void main(String[] args){ int[] arrays = {1,2,3,4,5,6}; String[] arrays2 ={"I","LOVE","YOU"}; arrays2=reverse(arrays2); printArray(arrays2); } public static void printArray(int[] arrays){ for(int array :arrays){ System.out.println(array); } } public static void printArray(String[] arrays){ for(String str:arrays){ System.out.println(str); } } public static int[] reverse(int[] list){ int[] result = new int[list.length]; for(int i = 0,j = result.length-1; i<list.length ; i++ ,j--){ result[j] =list[i]; } return result; } public static String[] reverse(String[] arr){ String[] result2 = new String[arr.length]; for(int i = 0,j = result2.length-1; i<arr.length ; i++ ,j--){ result2[j] =arr[i]; } return result2; } }