将数组元素颠倒的java代码

来源:互联网 发布:网络活动策划方案 编辑:程序博客网 时间:2024/05/17 16:11

有个数组a[n] 将数组元素颠倒:


import java.util.Arrays;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint [] a =new int [] {(int)(Math.random()*1000),(int)(Math.random()*1000),(int)(Math.random()*1000),(int)(Math.random()*1000),(int)(Math.random()*1000)};System.out.println(a);System.out.println(Arrays.toString(a));swap(a);System.out.println(Arrays.toString(a));}public static void swap(int[] a){for(int i=0;i<a.length/2;i++){int temp;temp = a[i];a[i] = a[a.length-i-1];a[a.length-i-1] = temp;}}}