编写一个方法,将数组传入进去之后将数组中的元素反转

public class Demo08 {
/*
需求:编写一个方法,将数组传入进去之后将数组中的元素反转. 例:传入[1,2,3] 反转:[3,2,1]
*/
public static void main(String[] args) {
int[] arr = {1, 2, 3};
reversal1(arr);

}

public static void reversal1(int[] arr) {

for (int min = 0, max = arr.length - 1; min <= max; min++, max--) {
int temp = arr[min];
arr[min] = arr[max];
arr[max] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}

posted on 2022-05-05 22:26  爱写代码的小振振  阅读(84)  评论(0)    收藏  举报

导航