首页 > 代码库 > 对给定数组进行反转

对给定数组进行反转

package cn.itcast.javase;

public class Test06 {
	/**
	 * 对给定数组进行反转
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[]={8,3,5,2,9,1};
		print(arr);
		inversion(arr);
		print(arr);
	}

	private static void inversion(int[] arr) {
		// TODO Auto-generated method stub
		for(int start=0,end=arr.length-1;start<end;start++,end--){
			swap(arr,start,end);
		}
	}

	private static void swap(int[] arr, int start, int end) {
		// TODO Auto-generated method stub
		int temp;
		temp=arr[start];
		arr[start]=arr[end];
		arr[end]=temp;
	}

	private static void print(int[] arr) {
		// TODO Auto-generated method stub
		System.out.print("[");
		for (int i = 0; i < arr.length; i++) {
			if(i!=arr.length-1)
				System.out.print(arr[i]+",");
			else
				System.out.println(arr[i]+"]");
		}
	}

}

  

对给定数组进行反转