java數組作為函數返回值


 1 //將一個二維數組行和列元素互換.存到另一個二維數組
 2 package test;
 3 
 4 public class test1_8 {
 5   public static int[][] huhuan(int[][] a) {
 6     int[][] b = new int[3][3];
 7     for (int i = 0; i < a.length; ++i) {
 8       for (int j = 0; j < a[i].length; ++j) {
 9         b[j][i] = a[i][j];
10       }
11     }
12     /*
13      * System.out.println("交換后的數組為:"); for (int j = 0; j < b.length; ++j) { for (int
14      * i = 0; i < b[j].length; ++i) { System.out.print(b[j][i] + " "); }
15      * System.out.println(); }
16      */
17     return b;
18   }
19 
20   public static void main(String[] args) {
21     // TODO Auto-generated method stub
22     int[][] a = new int[3][3];
23     for (int i = 0; i < a.length; ++i) {
24       for (int j = 0; j < a[i].length; ++j) {
25         a[i][j] = (int) (Math.random() * 100);
26       }
27     }
28     System.out.println("隨機產生的一個二維數組為:");
29     for (int i = 0; i < a.length; ++i) {
30       for (int j = 0; j < a[i].length; ++j) {
31         System.out.print(a[i][j] + " ");
32       }
33       System.out.println();
34     }
35     int[][] b = huhuan(a);
36     System.out.println("交換后的數組為:");
37     for (int j = 0; j < b.length; ++j) {
38       for (int i = 0; i < b[j].length; ++i) {
39         System.out.print(b[j][i] + " ");
40       }
41       System.out.println();
42     }
43   }
44 
45 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM