Bootstrap

Java基础数组(二维数组行列倒置排序方法)

public class ReverseSortDemo02 {
	public static void main(String[] args) throws Exception {
		int data[][] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		System.out.println("原数组:");
		print(data);
		System.out.println("置换后的数组:");
		reverdraSort(data);
	}

	// 二维数组列行倒置排序法
	public static void reverdraSort(int arr[][]) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = i; j < arr[i].length; j++) {
				if (i != j) {
					int temp = arr[i][j];
					arr[i][j] = arr[j][i];
					arr[j][i] = temp;
				}
			}
		}
		print(arr);

	}

	// 打印方法
	public static void print(int arr[][]) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "、");
			}
			System.out.println();
		}
		System.out.println();
	}
}

;