Java arrays swap basic ideas:
**
1. Calculate the number of reversed.
2. swap algorithm: for loop to achieve
<1> assign values to the left of the temporary variable.
<2> assign value on the right to the left.
<3> assign the value of the temporary variable to the right.
3. traverse the array to see if swap.
**
package lession11;
public class Demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array= {2,8,6,33,45,97,22,108,100};
//计算调换次数
int count=array.length/2;
//System.out.println(count);
//原数组的遍历
/*
for(int i=0;i<array.length;i++){
System.out.println("原数组:"+array[i]);
}
*/
for(int i=0;i<count;i++) {
int left=i;
int right=array.length-i-1;
//把左边的值赋值给临时变量
int tep=array[left];
//把右边的值赋值给左边
array[left]=array[right];
//把临时变量的值赋值给右边
array[right]=tep;
}
//遍历数组
for(int i=0;i<array.length;i++) {
System.out.println("调换后的数组:"+array[i]);
}
}
}
Please criticism, thank you.