數組是相同類型數據的有序集合。數組描述的是相同類型的若干個數據,按照必定的前後次序排列組合而成。其中,每個數據稱做一個元素,每一個元素能夠經過一個索引(下標)來訪問它們。數組的三個基本特色:java
提示數組
數組變量屬引用類型,數組也能夠當作是對象,數組中的每一個元素至關於該對象的成員變量。數組自己就是對象,Java中對象是在堆中的,所以數組不管保存原始類型仍是其餘對象類型,數組對象自己是在堆中存儲的學習
type [] array = {} //例如 int [] a = {1,2,3,4}; int [] b = new int[4];
注意事項this
public class Test { public static void main(String args[]) { int[] s = null; // 聲明數組; s = new int[10]; // 給數組分配空間; for (int i = 0; i < 10; i++) { s[i] = 2 * i + 1;//給數組元素賦值; System.out.println(s[i]); } } }
基本類型數組內存分配圖3d
class Man{ private int age; private int id; public Man(int id,int age) { super(); this.age = age; this.id = id; } } public class AppMain { public static void main(String[] args) { Man[] mans; //聲明引用類型數組; mans = new Man[10]; //給引用類型數組分配空間; Man m1 = new Man(1,11); Man m2 = new Man(2,22); mans[0]=m1;//給引用類型數組元素賦值; mans[1]=m2;//給引用類型數組元素賦值; } }
引用類型數組內存分配圖code
數組的初始化方式總共有三種:靜態初始化、動態初始化、默認初始化。下面針對這三種方式分別講解。
除了用new關鍵字來產生數組之外,還能夠直接在定義數組的同時就爲數組元素分配空間並賦值。對象
int[] a = { 1, 2, 3 };// 靜態初始化基本類型數組; Man[] mans = { new Man(1, 1), new Man(2, 2) };// 靜態初始化引用類型數組;
數組定義與爲數組元素分配空間並賦值的操做分開進行。blog
int[] a1 = new int[2];//動態初始化數組,先分配空間; a1[0]=1;//給數組元素賦值; a1[1]=2;//給數組元素賦值;
3.數組的默認初始化排序
數組是引用類型,它的元素至關於類的實例變量,所以數組一經分配空間,其中的每一個元素也被按照實例變量一樣的方式被隱式初始化。索引
int a2[] = new int[2]; // 默認值:0,0 boolean[] b = new boolean[2]; // 默認值:false,false String[] s = new String[2]; // 默認值:null, null
int [] a = {0,1,2,3,4} for(int i=0;i<a.lenth;i++){ System.out.println(a[i]); }
加強for循環for-each是JDK1.5新增長的功能,專門用於讀取數組或集合中全部的元素,即對數組進行遍歷。
String[] ss = { "aa", "bbb", "ccc", "ddd" }; for (String temp : ss) { System.out.println(temp); }
結果以下:
注意事項
System類裏也包含了一個static void arraycopy(object src,int srcpos,object dest, int destpos,int length)方法,該方法能夠將src數組裏的元素值賦給dest數組的元素,其中srcpos指定從src數組的第幾個元素開始賦值,length參數指定將src數組的多少個元素賦給dest數組的元素。
示例:數組拷貝
String[] s = {"阿里","尚學堂","京東","搜狐","網易"}; String[] sBak = new String[6]; System.arraycopy(s,0,sBak,0,s.length); for (int i = 0; i < sBak.length; i++) { System.out.print(sBak[i]+ "\t"); }
結果以下:
JDK提供的java.util.Arrays類,包含了經常使用的數組操做,方便咱們平常開發。Arrays類包含了:排序、查找、填充、打印內容等常見的操做。
import java.util.Arrays; public class Test { public static void main(String args[]) { int[] a = { 1, 2 }; System.out.println(a); // 打印數組引用的值; System.out.println(Arrays.toString(a)); // 打印數組元素的值; } }
注意
此處的Arrays.toString()方法是Arrays類的靜態方法,不是前面講的Object的toString()方法。
import java.util.Arrays; public class Test { public static void main(String args[]) { int[] a = {1,2,323,23,543,12,59}; System.out.println(Arrays.toString(a)); Arrays.sort(a); System.out.println(Arrays.toString(a)); } }
import java.util.Arrays; public class Test { public static void main(String[] args) { Man[] msMans = { new Man(3, "a"), new Man(60, "b"), new Man(2, "c") }; Arrays.sort(msMans); System.out.println(Arrays.toString(msMans)); } } class Man implements Comparable { int age; int id; String name; public Man(int age, String name) { super(); this.age = age; this.name = name; } public String toString() { return this.name; } public int compareTo(Object o) { Man man = (Man) o; if (this.age < man.age) { return -1; } if (this.age > man.age) { return 1; } return 0; } }
Comparable接口裏定義了一個compareTo(Object obj)方法,該方法返回一個整數值,實現該接口的類必須實現該方法,實現了該接口的類的對象就能夠比較大小。當一個對象調用該方法與另外一個對象進行比較時,例如obj1.compareTo(obj2),若是該方法返回0,則表示兩個對象相等,若是該方法返回一個正整數,則代表obj1大於obj2;若是該方法返回一個負整數,則代表obj1小於obj2。
下面是一些已經實現了Comparable接口的經常使用類:
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] a = {1,2,323,23,543,12,59}; System.out.println(Arrays.toString(a)); Arrays.sort(a); //使用二分法查找,必須先對數組進行排序; System.out.println(Arrays.toString(a)); //返回排序後新的索引位置,若未找到返回負數。 System.out.println("該元素的索引:"+Arrays.binarySearch(a, 12)); } }
import java.util.Arrays; public class Test { public static void main(String[] args) { int[] a= {1,2,323,23,543,12,59}; System.out.println(Arrays.toString(a)); Arrays.fill(a, 2, 4, 100); //將2到4索引的元素替換爲100; System.out.println(Arrays.toString(a)); } }
多維數組能夠當作以數組爲元素的數組。能夠有二維、三維、甚至更多維數組,可是實際開發中用的很是少。最多到二維數組(學習容器後,咱們通常使用容器,二維數組用的都不多)。
public class Test { public static void main(String[] args) { // Java中多維數組的聲明和初始化應按從低維到高維的順序進行 int[][] a = new int[3][]; a[0] = new int[2]; a[1] = new int[4]; a[2] = new int[3]; // int a1[][]=new int[][4];//非法 } }
public class Test { public static void main(String[] args) { int[][] a = { { 1, 2, 3 }, { 3, 4 }, { 3, 5, 6, 7 } }; System.out.println(a[2][3]); } }
import java.util.Arrays; public class Test { public static void main(String[] args) { int[][] a = new int[3][]; // a[0] = {1,2,5}; //錯誤,沒有聲明類型就初始化 a[0] = new int[] { 1, 2 }; a[1] = new int[] { 2, 2 }; a[2] = new int[] { 2, 2, 3, 4 }; System.out.println(a[2][3]); System.out.println(Arrays.toString(a[0])); System.out.println(Arrays.toString(a[1])); System.out.println(Arrays.toString(a[2])); } }
//獲取的二維數組第一維數組的長度。 System.out.println(a.length); //獲取第二維第一個數組長度。 System.out.println(a[0].length);