數組對每門編程語言都是重要的數據結構之一,java語言提供的數組是用來儲存固定大小的同類型元素的。
當你需要保存一組數據類型相同的變量或者對象時,我們不可能給每個變量都定義一個變量名,這樣的操作會使代碼臃腫、工作量大且無意義。這時我們就需要數組來保存這些數據。數組根據需要的不同分為一維數,二維數組和多維數組。下面我們來看下數組的聲明、創建和初始化。
一、數組的聲明
要在程序中使用數組,就需要先聲明數組,下面是聲明數組變量的語法:
一位數組的聲明方法:
type[] 數組名;(首選方法)
type 數組名[]; (效果一樣,但不推薦)
數組在聲明時不能指定器長度(數組中元素的個數)
二、數組的創建
Java中使用new關鍵字來創建數組的,語法如下:
方式1(推薦,更能表現現數組的類型)
type[] 數組名 = new type[數組中元素個數];
eg:
int[] a = new int[3];
數組名,也即應用a,指向數組元素的首地址。
方式2(定義時直接初始化)
type[] 變量名 = new type[]{逗號分隔的數組元素}
其中紅色部分可以省掉,所以又分兩種:
1、int[] num=new int[]{1,2,2,3,3};
2、int[] num1= {1,3,6,87,4};
其中 int[] num=new int[]{1,2,2,3,3};第二個方括號不能加數組長度,因為數組個數是由後面花括號決定的。
Java中的每個數組都有length屬性,表示數組的長度。
length屬性是public final int的,即length是只讀的。數組長度一旦確定,就不能改變大小。
二維數組的定義方法:
1 int[][] table = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 5, 6, 3 } };
2
3 int[][] table1 = null;
4 table1 = new int[3][3];
5
6 int[][] table2 = new int[3][3];
7 table2[0][0] = 11;
8 table2[0][1] = 121;
9 table2[0][2] = 11;
10 table2[1][0] = 11;
11 table2[1][1] = 121;
12 table2[1][2] = 11;
13 table2[2][0] = 11;
14 table2[2][1] = 121;
15 table2[2][2] = 11;
16 System.out.println();
二維數組的遍歷
1 for (int row = 0; row < table.length; row++) {
2 for (int column = 0; column < table[row].length; column++) {
3 System.out.print(table[row][column] + "\t");
4 }
5 System.out.println();
6 }
1.動態初始化:數組定義與為數組分配空間和賦值的操作分開進行;
2.靜態初始化:在定義數字的同時就為數組元素分配空間並賦值;
3.默認初始化:數組是引用類型,它的元素相當於類的成員變量,因此數組分配空間後,每個元素也被按照成員變量的規則被隱士初始化。
動態實例:
1 public class Arrays
2 {
3 public static void main(String args[]) {
4 int a[] ;
5 a = new int[3] ;
6 a[0] = 0 ;
7 a[1] = 1 ;
8 a[2] = 2 ;
9 String string[] ;
10 string= new String[3] ;
11 string[0] = "張三";
12 string[1] = "李四" ;
13 string[2] = "小明";
14 }
15 }
靜態實例:
1 public class Arrays
2 {
3 public static void main(String args[]) {
4 int a[] = {0,1,2} ;
5 String[] string = {"張三","李四","小敏"} ;
6 }
7 }
默認實例:
1 public class Arrays
2 {
3 public static void main(String args[]) {
4 int[] a = new int [5] ;
5 System.out.println("" + a[3]) ;
6 }
7 }
處理數組:
數組的元素和數組大小是確定的,並且數組的元素有多個,所以當處理數組時,我們通常使用基本for循環和增強for循環
示例:
該實例完整地展示了如何創建、初始化和操縱數組:

JDK 1.5 引進了一種新的循環類型,它能在不使用下標的情況下遍歷數組。
該實例用來顯示數組myList中的所有元素:
1 public class Array {
2
3 public static void main(String[] args) {
4 int[] List = {1,9,2,9,3,4,3,5};
5
6 // 打印所有數組元素
7 for (int a:List) {
8 System.out.println(a);
9 }
10 }
11 }
1、使用for循環復制

2、copyOf方法復制數組

3、將指定數組的指定范圍復制到一個新數組(copyOfRange)
1 public class Text {
2 public static void main(String[] args) {
3 int[] a = { 2, 4, 2, 77, 22, 777, 34 };
4 int[] b = Arrays.copyOfRange(a, 0, 3);
5 for (int n : b) {
6 System.out.print(n + " ");
7 }
8 }
9 }

使用sort方法排序
1 public class Text {
2 public static void main(String[] args) {
3 int[] a = { 2, 4, 2, 77, 22, 777, 34 };
4 Arrays.sort(a);
5 for(int b:a){
6 System.out.print(b+" ");
7 }
8 }
9
10 }

如果兩個數組以相同順序包含相同的元素,則兩個數組是相等的。此外,如果兩個數組引用都為 null,則認為它們是相等的。
1 public class Text {
2 public static void main(String[] args) {
3 int[] a = { 2, 4, 2, 77, 22, 777, 34 };
4 int[] b = { 2, 4, 2, 77, 22, 777, 34 };
5 int[] c = { 4, 2, 22, 77, 2, 777, 34 };
6
7 boolean flag = Arrays.equals(a, b);
8 System.out.println(flag);
9
10 boolean flag1=Arrays.equals(a, c);
11 System.out.println(flag1);
12 }
13 }
以上實例編譯運行結果如下:
true
false