//定义一个数组,交换首尾索引对应的元素
public class ArrayTest {
public static void main(String[] args) {
int [] array6 = {1,3,5,7,9,0};
for (int i = 0; i < array6.length; i++) {
//语句1
System.out.print(array6[i]+" ");
}
//首先要清楚,0索引对应5索引,也就是数组长度-1
//以6个元素为例,0对5,1对4,2对3,等于就结束了
//所以令i=0,j等于索引长度-1,当i>j时,交换结束
for (int i = 0,j=array6.length-1;
i > j; i++,j--) {
//交换元素需要第三个临时值
int temp = array6[i];
//把大索引的值赋给小索引
array6[i]=array6[j];
array6[j]=temp;
}
System.out.println();//这个打印空白的作用是让语句1和语句2分行
for (int i = 0; i < array6.length; i++) {
//语句2
System.out.print(array6[i]+" ");
}
}}