天天看點

Java數組

Java數組要求所有的數組元素具有相同的資料類型

Java數組可以存儲基本類型與引用類型資料

數組初始化分為靜态初始化與動态初始化

// 定義一維數組

type[] arrayName;

// 初始化形式一

arrayName = new type[]{…};

// 初始化形式二

arrayName = new type[length];

當然可以在定義一維數組的時候進行初始化操作

type[] arrayName = new type[]{…};

type[] arrayName = new type[length];

除了以上兩種情形外 還有第三種情形

type[] arrayName = {…};

對于這種情形 必須同時完成數組的定義和初始化操作

// 定義二維數組

type[][] arrayName;

arrayName = new type[][]{{…},{…},…};

// 初始化形式二 前後兩個length可以不同

arrayName = new type[length][]; 或 arrayName = new type[length][length];

當然可以在定義二維數組的時候進行初始化操作

type[][] arrayName = new type[][]{…};

type[][] arrayName = new type[length][]; 或 type[][] arrayName = new type[length][length];   前後兩個length可以不同

繼續閱讀