翻转数组
在看Collections工具类代码时,发现翻转数组的逻辑,感觉很有趣。思想:折半交换
public static void main(String[] args) { // TODO Auto-generated method stub int[] n = new int[] {1,2,3,4,5,4,6,7,8,9}; int size = n.length; for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) swap(n, i, j); for(int i : n) { System.out.print(i + " "); } } private static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
这是水木竹水的博客