1. 程式人生 > >Java 二維陣列反轉

Java 二維陣列反轉

通過交換下標實現反轉操作:


public class TestFile {

	public static void main(String[] args) {
		int[][] arr={{1,2,3},{4,5,6},{7,8,9}};
		int[][] toarr=new int[3][3]; 
		
		System.out.println("反轉前");
		
		int k=0;		
		for(int[] x:arr){
			for(int i=0;i<x.length;i++){
				System.out.print(x[i]+" ");
			    toarr[i][k]= arr[k][i];
			}
			k++;
			System.out.println("");
		}
		
		System.out.println("反轉後");
		
		for(int[] x:toarr){
			for(int i=0;i<x.length;i++){				
				System.out.print(x[i]+" ");
			}
			k++;
			System.out.println("");
		}
	}
	
	static void Print(String outinfo){
		System.out.println(outinfo); 
	}
}