[2014-07-25]JAVA筆記_數組(Array)

一、數組(Array):相同類型數據的集合就叫數組。

二、數組定義: type[] 變量名 = new type[數組長度]; 例如定義長度爲 10 的數組: int[] a = new int[10]; 或者 int a[] = new int[10];

三、數組中的元素索引是從 0 開始的。 對於數據來說,最大的索引 == 數組長度 -1

四、定義數組的第3種方式:type[] 變量名 = {new type[]} {逗號分隔的初始化值列表};

五、 Java 中的每個數組都有一個名爲 length 的屬性,表示數組的長度。length 屬性是 public,final,int 的。數組長度一旦確定,就不能改變大小。

六、 int[] a = new int[10],其中 a 是一個引用, 它指向了生成的數組對象的首地址,數組中每個元素都是 int 類型, 其中僅存放數值本身


//Array 
public class ArrayTest{
	public static void main(String[] args){
		/* 第一種定義方式
		int[] a = new int[4];	//有new關鍵字說明生產了對象,所以數組屬於引用數據類型。

		a[0] = 1;
		a[1] = 2;
		a[2] = 3;
		a[3] = 4;

		System.out.println(a[2]);
		*/

		// 第二種定義方式
		int a[] = new int[2];	//第一種方式較好。因爲int[]是作爲一個整體,意思也就是是定義了一個int[] 這種類型的變量a.

		a[0] = 1;
		a[1] = 2;
		System.out.println(a[1]);

		//第三種定義方式
		int[] b = {1, 2, 3, 4};

		System.out.println(b[2]);

		int[] c = new int[]{1,2,3,4};

		System.out.println(b[3]);


		//length 屬性
		int[] d = new int[10];
		for(int i = 0; i < d.length; i++){
			d[i] = i + 1;
			System.out.println(d[i]);
		}

		//數組默認初始值
		int[] e = new int[4];
		System.out.println(e[3]);	// 0

		boolean[] f = new boolean[3];
		System.out.println(f[2]);	//false

		//數組比較
		int[] g = {1, 2, 3};
		int[] h = {1, 2, 3};
		
		System.out.println(g.equals(h));	//false.這個equals()方法是繼承Object,數組並沒有 override這個方法,比較的是地址,而不是內容。



	}
}




//引用數據類型
public class ArrayTest2{
	public static void main(String[] args){
		
		Person[] p = new Person[3];	//聲明Person類型數組。沒有生成對象,生成了3個引用,p還是指向首地址
		
		System.out.println(p[0]);	//引用類型成員變量初值null

		p[0] = new Person(10);         //p[0]引用保存的new返回的地址
		p[1] = new Person(20);
		p[2] = new Person(30);

		for(int i = 0; i < p.length; i++){
		
			System.out.println(p[i].age);
		}

		Person[] p2 = new Person[5];

		for(int i = 0; i < p2.length; i++){
		
			System.out.println(p2[i]);
		}


	}
}

class Person{

	int age;

	public Person(int age){
		this.age = age;
	}
}




public class ArrayTest3{
	public static void main(String[] args){
	
		Student[] s = new Student[10];

		for(int i = 0; i < s.length; i++){
		
			s[i] = new Student();
			s[i].name = i % 2 == 0 ? "zhangsan":"lisi";
		}

		for(int i = 0; i < s.length; i++){
		
			System.out.println(s[i].name);
		}

	}
}

class Student{

	String name;
}

七、二維數組。二維數組是一種平面的二維結構,本質上是數組的數組。 二維數組的定義方式:type[][] a = new type[2][3];


//二維數組
public class ArrayTest4{
	public static void main(String[] args){
		
		int[][] a = new int[2][3];

		System.out.println(a instanceof Object);
		System.out.println(a[0] instanceof int[]);	//驗證i[0]是一個整型數組
		int m = 0;
		for(int i = 0; i < 2; i++){
		
			for(int j = 0; j < 3; j++){
				m++;
				a[i][j] = 2 * m;
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
		

	}
}



//不規則數組
public class ArrayTest6{
	public static void main(String[] args){
	
		/*
		int[][] a = new int[3][];

		a[0] = new int[2];
		a[1] = new int[3];
		a[2] = new int[1];
		*/

	//	int[][] b = new int[][3];	//Error
	
		int[][] b = new int[][]{ {1, 2, 3}, {4}, {5, 6, 7, 8} };

		for(int i = 0; i < b.length; i++){
			for(int j = 0; j < b[i].length; j++){
			
				System.out.print(b[i][j]+ " ");

			}
			System.out.println();
		}
	}
}

Example:

//元素互換
public class Swap3{

	public static void swap(char[] ch, char c){

		ch[0] = 'C';	//ch引用指向數組a的首地址
		c = 'D';

	}

	public static void main(String[] args){
	
		char[] a = {'A', 'B'};
		swap(a, a[1]);	//傳遞數組的引用,和數組的第2個值
		for(int i = 0; i < a.length; i++){
		
			System.out.println(a[i]);
		}

	}
}







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