class ArrayReversal
{
public static void main(String[] args)
{
int[] array = {1,2,3,4,5,6,7,8,9};
for (int i = 0; i < array.length / 2; i++)
{
/*方法一:*/
/*
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1- i] = temp;
*/
/*方法二:更高效*/
array[i] = array[i] ^ array[array.length - 1 - i];
array[array.length - 1 - i] = array[i] ^ array[array.length - 1 - i];
array[i] = array[i] ^ array[array.length - 1 - i];
}
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + "\t");
}
}
}
Java数组反转
最新推荐文章于 2024-08-23 21:37:43 发布