Java數組

Java數組

一、數組的概述

  1. 數組可以看成是多個相同類型數據集合,對這些數據統一管理。
  2. 數組變量屬引用類型,數組也可以看成是對象,數組中的每個元素相當於該對象的成員變量。
  3. 數組中的元素可以是任何數據類型,基本數據類型和引用類型都行。

二、一維數組的聲明

  1. 一維數組的聲明方式:
type var[];//或者
type[] var;//或者
type []var;//這三種方式都可以
例如:
int a[];
int []b;
double c[];
String s1[];
Person[] p;//注意這是一個Person類型的數組,裏面放的是Person對象的引用

注意:Java語言中聲明數組時不能指定其長度(數組中元素的個數),例如:int a [5];//非法

三、數組對象的創建

Java中使用關鍵字new創建數組對象,格式爲:
數組名 = new 數組元素類型[數組元素的個數]

四、元素爲引用數據類型的數組

注意:元素爲引用數據類型的數組中的每一個元素都需要實例化!具體看下面的例子:
數組爲引用類型的內存分配

五、數組初始化

  1. 動態初始化,數組定義與數組元素分配空間和賦值的操作分開進行,如:
/**
 * Java 數組動態初始化
 */
public class ArrayDynamicInit {
    public static void main(String[] args) {
        int a[];
        a = new int[3];
        a[0] = 3;
        a[1] = 4;
        a[2] = 5;
        Date date[];
        date = new Date[3];
        date[0] = new Date(2019,7,9);
        date[1] = new Date(2019,7,10);
        date[2] = new Date(2019,7,11);
    }
    
    static class Date{
        int year,month,day;
        Date(int year,int month,int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }
}
  1. Java數組靜態初始化,在定義數組的同時就爲數組元素分配空間並賦值,示例代碼如下:
/**
 * Java數組靜態初始化
 */
public class ArrayStaticInit {

    public static void main(String[] args) {
        int a[] = {1, 2, 3};
        Date date[] = {new Date(2019, 7, 9),
                new Date(2019, 7, 10),
                new Date(2019, 7, 11)};
    }

    static class Date {
        int year, month, day;
        
        Date(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }
}

六、數組元素的默認初始化

數組是引用類型,它的元素相當於類的成員變量,因此數組分配空間後,每個元素也被按照成員變量的規則被初始化。示例代碼如下:

/**
 * Java數組元素默認初始化
 */
public class InitArrayElem {

    public static void main(String[] args) {
        int a[] = new int[3];
        System.out.println(a[0]);
        Date date[] = new Date[3];
        System.out.println(date[1]);
    }

    static class Date {
        int year, month, day;

        Date(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }
}

//輸出結果爲:
輸出結果

七、數組元素的引用

數組元素的引用

八、二維數組的聲明

二維數組的聲明二維數組必須先聲明一維(左側),否則是非法的。具體看下面代碼:

public class TwoDimensionArrayInit {
    //靜態初始化
    int array1[][] = {{1,2},{2,3,4},{7,3}};
    int array2[3][2] = {{1,2},{2,3},{4,5}};//非法

    //動態初始化
    int array3[][] = new int[3][5];//聲明時指定一維和二維所佔空間
    //
    int array4[][] = new int[3][];
}

九、遍歷二維數組

/**
 * 遍歷二維數組
 */
public class TwoDimensionArrayInit {

    public static void main(String[] args) {
        int array[][] = {{1, 2}, {3, 4, 5, 6}, {7, 8, 9}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
        }
    }
}

輸出結果:
輸出結果

十、數組的拷貝

使用java.lang.Sytem類的靜態方法

public static void arrarcopy(Object src,int srcPos,Object dest,
											int desPos,int length)

可以用於數組src從第srcPos項元素開始的length個元素拷貝到目標數組從desPos項開始的length個位置。如果源數據數目超過目標數組邊界會拋出IndexOutOfBoundsException異常。

/**
 * 拷貝一維數組
 */
public class TwoDimensionArrayInit {

    public static void main(String[] args) {
        String array[] = {"Alibaba", "Tencent", "Baidu", "Jingdong"};
        String companyArray[] = new String[5];
        System.arraycopy(array, 0, companyArray, 0, array.length);
        for (int i = 0; i < companyArray.length; i++) {
            System.out.print(companyArray[i] + " ");
        }
    }
}

輸出結果:
輸出結果由於原數組長度只有4,所有最後一個元素未賦值,默認爲null,複製二維數組的代碼如下:

/**
 * 複製二維數組
 */
public class CopyTwoDimensionArray {

    public static void main(String[] args) {
        int array[][] = {{1, 2}, {1, 2, 3}, {3, 4}};
        int arrayBak[][] = new int[3][];
        System.arraycopy(array, 0, arrayBak, 0, array.length);
        arrayBak[2][1] = 14;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
        }
    }
}

輸出結果:
輸出結果

==由於arraybak複製了array,在內存中arraybak和array指向的是同一個引用,也就是說操作arraybak也就相當於操作了array,arraybak把4的值修改爲14,array的值也就跟着發生了改變(通過畫內存圖一目瞭然,一般人我不告訴他,嘿嘿)。
程序執行結束後的內存圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章