數組學習

一、綜述

       數組代表一系列對象或者基本數據類型,所有相同的類型都封裝到一起——採用一個統一的標識符名稱。
       數組的創建實際是在運行期間進行的。

二、定義和初始化

       數組的定義和使用是通過方括號索引運算符進行的([ ])
       e.g.1       int[ ] al;
                       int al[ ];
              這時只是定義了指向數組的一個句柄,而且尚未給數組分配任何空間。
              爲了給數組創建相應的存儲空間,必須編寫一個初始化表達式。
       e.g.2       int[] a1 = { 1, 2, 3, 4, 5 };

三、用法Example(含講解)

       1、複製句柄

public class Arrays {
  public static void main(String[] args) {
    int[] a1 = { 1, 2, 3, 4, 5 };
    int[] a2;
    a2 = a1;
    for(int i = 0; i < a2.length; i++)
      a2[i]++;
    for(int i = 0; i < a1.length; i++)
      prt("a1[" + i + "] = " + a1[i]);
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

(1)a1獲得了一個初始值,而a2沒有;a2將在以後賦值——這種情況下是賦給另一個數組。
(2)length: 所有數組都有這個本質成員(無論它們是對象數組還是基本類型數組),可以用它來獲知數組內包含了多少個元素。
                           由於Java數組從元素0開始計數,所以能索引的最大元素編號是“length-1”。

       2、創建一個非基本類型對象的數組

import java.util.*;

public class ArrayClassObj {
  static Random rand = new Random();
  static int pRand(int mod) {
    return Math.abs(rand.nextInt()) % mod + 1;
  }
  public static void main(String[] args) {
    Integer[] a = new Integer[pRand(20)];
    prt("length of a = " + a.length);
    for(int i = 0; i < a.length; i++) {
      a[i] = new Integer(pRand(500));
      prt("a[" + i + "] = " + a[i]);
    }
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

(1)在new調用後纔開始創建數組:Integer[] a = new Integer[pRand(20)];
(2)它只是一個句柄數組,而且除非通過創建一個新的Integer對象,從而初始化了對象句柄,否則初始化進程不會結束:a[i] = new Integer(pRand(500));
           但若忘記創建對象,就會在運行期試圖讀取空數組位置時獲得一個“違例”錯誤。

       3、初始化對象數組一(Java 1.0允許的唯一形式);
             初始化對象數組二(自Java 1.1纔開始提供支持);

public class ArrayInit {
  public static void main(String[] args) {
    Integer[] a = {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };

    // Java 1.1 only:
    Integer[] b = new Integer[] {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };
  }
}

(1)這種做法大多數時候都很有用,但限制也是最大的,因爲數組的大小是在編譯期間決定的。
(2)初始化列表的最後一個逗號是可選的(這一特性使長列表的維護變得更加容易)。

       4、Object數組
              由於所有類最終都是從通用的根類Object中繼承的,所以能創建一個方法,令其獲取一個Object數組

// Using the Java 1.1 array syntax to create
// variable argument lists

class A { int i; }

public class VarArgs {
  static void f(Object[] x) {
    for(int i = 0; i < x.length; i++)
      System.out.println(x[i]);
  }
  public static void main(String[] args) {
    f(new Object[] {
        new Integer(47), new VarArgs(),
        new Float(3.14), new Double(11.11) });
    f(new Object[] {"one", "two", "three" });
    f(new Object[] {new A(), new A(), new A()});
  }
}

       5、多維數組

//: 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);
  }
}

(1)第一個例子展示了基本數據類型的一個多維數組。我們可用花括號定出數組內每個矢量的邊界:
          int[][] a1 = {
            { 1, 2, 3, },
            { 4, 5, 6, },
          };
(2)第二個例子展示了用new分配的一個三維數組。在這裏,整個數組都是立即分配的。
(3)第三個例子向大家揭示出構成矩陣的每個矢量都可以有任意的長度:
         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)];
         }
(4)第四個例子,它向我們演示了用花括號收集多個new表達式的能力。
(5)第五個例子展示瞭如何逐漸構建非基本類型的對象數組
          i*j只是在Integer裏置了一個有趣的值。

發佈了35 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章