????需求 ????
编写一个程序,其功能是能随机生成一个数组、同时完成对该数组的转置并打印输出转置后的数组。
????代码 ????
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import static java.lang.System.out; import java.util.Random;
public class Reverse2dArray { static int[][] a;
public Reverse2dArray(int r, int c){ a = new int[r][c]; buildRandArray(); }
public void buildRandArray(){ Random random = new Random(); for(int i = 0; i < a.length; i++) for(int j = 0; j < a[i].length; j++) a[i][j] = random.nextInt(100); }
public int[][] reverse2dArray(){ int [][] b = new int [a[0].length] [a.length] ; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { b[j][i] = a[i][j] ; } } return b ; } public void showArray(int[][] c){ out.printf("=========%2d×%-2d=========%n", c.length, c[0].length); for(int[] cr : c){ for(int cc : cr){ out.printf("%5d ", cc); } out.println(); } out.println("======================="); } public static void main(String[] args) { int row = 3, col = 4; Reverse2dArray me = new Reverse2dArray(row, col); me.showArray(a); int[][] r = me.reverse2dArray(); me.showArray(r); } }
|
????截图 ????
