/**
* 数组逆序:
* 将一个数组中的索引进行逆序排序显示
* 操作步骤:
* 1、建立一个数组
* 2、对数组进行遍历
* 3、对数组中国索引进行互换
* 4、遍历数组显示互换之后数据
*/
public class LoopTest5 {
public static void main(String[] args) {
int[] arr = {123,321,456,324,567,543,190,987};
//调用数组逆序方法
test1(arr);
//看到数组的元素,遍历
runTest(arr);
}
/**
* 定义方法:数显数组逆序
* 1、返回值,无返回值
* 2、参数,数组即使参数
* **/
public static void test1(int[] arr){
//for的第一项,定义2个变量,最后,两个变量++ --
for (int min = 0 , max = arr.length-1 ; min<max ; min++, max--){
//定义一个中间变量,保存min索引
int tem = arr[min];
//max索引上的元素,赋值给min索引
arr[min] = arr[max];
//临时变量,保存的数字,赋值到max索引
arr[max] = tem;
}
}
//按照[]方式输出遍历数组数信息
public static void runTest(int[] arr){
System.out.print("[");
//for循环
for (int i = 0 ; i < arr.length; i++){
//判断数组是否是最后一位,是则打印],否则打印,
if (i==arr.length-1){
System.out.print(arr[i] + "]");
}else {
System.out.print(arr[i] + ",");
}
}
}
}
原文:https://www.cnblogs.com/lcj0703/p/10480572.html