java中建立多維陣列

來源:互聯網
上載者:User

在Java裡可以方便地建立多維陣列:
 

//: MultiDimArray.java// Creating multidimensional arrays.import java.util.*;public class MultiDimArray {  static Random rand = new Random();  static int pRand(int mod) {    return Math.abs(rand.nextInt()) % mod + 1;  }  public static void main(String[] args) {    int[][] a1 = {      { 1, 2, 3, },      { 4, 5, 6, },    };    for(int i = 0; i < a1.length; i++)      for(int j = 0; j < a1[i].length; j++)        prt("a1[" + i + "][" + j +            "] = " + a1[i][j]);    // 3-D array with fixed length:    int[][][] a2 = new int[2][2][4];    for(int i = 0; i < a2.length; i++)      for(int j = 0; j < a2[i].length; j++)        for(int k = 0; k < a2[i][j].length;            k++)          prt("a2[" + i + "][" +              j + "][" + k +              "] = " + a2[i][j][k]);    // 3-D array with varied-length vectors:    int[][][] a3 = new int[pRand(7)][][];    for(int i = 0; i < a3.length; i++) {      a3[i] = new int[pRand(5)][];      for(int j = 0; j < a3[i].length; j++)        a3[i][j] = new int[pRand(5)];    }    for(int i = 0; i < a3.length; i++)      for(int j = 0; j < a3[i].length; j++)        for(int k = 0; k < a3[i][j].length;            k++)          prt("a3[" + i + "][" +              j + "][" + k +              "] = " + a3[i][j][k]);    // Array of non-primitive objects:    Integer[][] a4 = {      { new Integer(1), new Integer(2)},      { new Integer(3), new Integer(4)},      { new Integer(5), new Integer(6)},    };    for(int i = 0; i < a4.length; i++)      for(int j = 0; j < a4[i].length; j++)        prt("a4[" + i + "][" + j +            "] = " + a4[i][j]);    Integer[][] a5;    a5 = new Integer[3][];    for(int i = 0; i < a5.length; i++) {      a5[i] = new Integer[3];      for(int j = 0; j < a5[i].length; j++)        a5[i][j] = new Integer(i*j);    }    for(int i = 0; i < a5.length; i++)      for(int j = 0; j < a5[i].length; j++)        prt("a5[" + i + "][" + j +            "] = " + a5[i][j]);  }  static void prt(String s) {    System.out.println(s);  }} ///:~


用於列印的代碼裡使用了length,所以它不必依賴固定的數組大小。
第一個例子展示了基礎資料型別 (Elementary Data Type)的一個多維陣列。我們可用花括弧定出數組內每個向量的邊界:

int[][] a1 = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};

每個方括弧對都將我們移至數組的下一級。
第二個例子展示了用new分配的一個三維數組。在這裡,整個數組都是立即分配的:
int[][][] a2 = new int[2][2][4];
但第三個例子卻向大家揭示出構成矩陣的每個向量都可以有任意的長度:

 

    int[][][] a3 = new int[pRand(7)][][];    for(int i = 0; i < a3.length; i++) {      a3[i] = new int[pRand(5)][];      for(int j = 0; j < a3[i].length; j++)        a3[i][j] = new int[pRand(5)];    }


對於第一個new建立的數組,它的第一個元素的長度是隨機的,其他元素的長度則沒有定義。for迴圈內的第二個new則會填寫元素,但保持第三個索引的未定狀態——直到碰到第三個new。
根據輸出結果,大家可以看到:假若沒有明確指定初始化值,數組值就會自動初始化成零。
可用類似的表式處理非基本類型對象的數組。這從第四個例子可以看出,它向我們示範了用花括弧收集多個new運算式的能力:

 

    Integer[][] a4 = {      { new Integer(1), new Integer(2)},      { new Integer(3), new Integer(4)},      { new Integer(5), new Integer(6)},    };


第五個例子展示了如何逐漸構建非基本類型的對象數組:

 

    Integer[][] a5;    a5 = new Integer[3][];    for(int i = 0; i < a5.length; i++) {      a5[i] = new Integer[3];      for(int j = 0; j < a5[i].length; j++)        a5[i][j] = new Integer(i*j);    }


i*j只是在Integer裡置了一個有趣的值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.