數(shù)組的基本概念
1、數(shù)組是由多個數(shù)據(jù)類型相同的元素組成的有順序的數(shù)據(jù)集合。
2约急、數(shù)組是屬于引用數(shù)據(jù)類型(復(fù)雜數(shù)據(jù)類型)零远。
3、數(shù)組中的每一個數(shù)據(jù)叫做數(shù)組的一個元素(element)厌蔽。
4牵辣、數(shù)組中的元素可以是任何數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型奴饮。
5纬向、數(shù)組是固定的,不能擴(kuò)展戴卜。
6逾条、數(shù)組中元素的個數(shù),叫做數(shù)組的長度(length)投剥。
7师脂、通過數(shù)組名和下標(biāo)可以訪問數(shù)組的各元素
8、數(shù)組可以分為一維數(shù)組和多維數(shù)組江锨。
一維數(shù)組
1吃警、數(shù)組的聲明
(1)數(shù)組類型 [ ] 數(shù)組名 ;
int[ ] a ;
(2)數(shù)組類型 數(shù)組名[ ] ;
double b[ ] ;
2、數(shù)組的創(chuàng)建
利用 new 來給數(shù)組型變量分配內(nèi)存空間
數(shù)組名 = new 數(shù)據(jù)類型 [ 數(shù)組長度 ] ;
a = new int [ 8 ] ;
b = new double [ 5 ] ;
3啄育、數(shù)組的聲明和創(chuàng)建可以同時進(jìn)行
(1)數(shù)組類型[ ] 數(shù)組名 = new 數(shù)據(jù)類型 [ 數(shù)組長度 ] 酌心;
int[ ] a = new int [10 ];
(2)數(shù)組類型 數(shù)組名[ ] = new 數(shù)據(jù)類型 [ 數(shù)組長度 ] ;
double b[ ] = new double [ 5 ] ;
4挑豌、數(shù)組的內(nèi)存模型
(1)數(shù)組是存儲多個相同數(shù)據(jù)類型變量的對象谒府。數(shù)組的所有元素都保存在堆內(nèi)存中。
(2)創(chuàng)建一個數(shù)組就是在堆中創(chuàng)建一個數(shù)組對象浮毯。
(3)數(shù)組創(chuàng)建后立即擁有默認(rèn)值
(4)數(shù)組的索引是從 0 開始完疫。
(5)數(shù)組在堆中是連續(xù)分配內(nèi)存。
5债蓝、數(shù)組的初始化
(1)在創(chuàng)建數(shù)組的同時就為數(shù)組元素分配內(nèi)存空間并賦值壳鹤。
(2)如果數(shù)組類型是 int 型,所有元素自動初始化為 0 饰迹。
(3)如果數(shù)組類型是 boolean型芳誓,所有元素自動初始化為 false ;
(1)數(shù)組類型[ ] 數(shù)組名 = { 元素1 , 元素2 , ... } 啊鸭;
int[ ] a = { 1 , 2 , 3 , 4 , 5 } ;
(2)數(shù)組類型[ ] 數(shù)組名 = new 數(shù)據(jù)類型 [ ] { 元素1 , 元素2 , ... } 锹淌;
int[ ] a = new int[ ] { 1 , 2 , 3 , 4 , 5 } ;
(3)使用循環(huán)初始化
int[ ] a = new int[ 5 ] ;
for (int i = 0 ; i < a.length ; i ++) { // length:表示數(shù)組的長度
a[i] = i;
}
//Arrays.toString():一次性將數(shù)組中所有的數(shù)據(jù)全部輸出
System.out.println(Arrays.toString(a));
二維數(shù)組
1、數(shù)組的聲明
(1)數(shù)組類型 [ ][ ] 數(shù)組名 ;
int[ ][ ] a ;
(2)數(shù)組類型 數(shù)組名[ ][ ] ;
double b[ ][ ] ;
(3)數(shù)組類型[ ] 數(shù)組名[ ] ;
double[ ] c[ ] ;
2赠制、數(shù)組的創(chuàng)建
利用 new 來給數(shù)組型變量分配內(nèi)存空間
(1)數(shù)組名 = new 數(shù)據(jù)類型 [ 行數(shù) ][ 列數(shù) ] ;
a = new int [ 8 ][ 9 ] ;
(2)數(shù)組名 = new 數(shù)據(jù)類型 [ 行數(shù) ][ ] ;
b = new double [ 5 ][ ] ;
赂摆!注:必須聲明行的個數(shù)。
3、數(shù)組的初始化
(1)數(shù)組類型[ ][ ] 數(shù)組名 = {{元素11, 元素12, ... },{元素21, 元素22 , ...} , ... } 烟号;
int[ ][ ] a = { {1 , 2 , 3} ,{ 4 , 5},{ 6, 7, 8, 9 } } ;
(2)數(shù)組類型[ ] 數(shù)組名 = new 數(shù)據(jù)類型[ ]{{元素11,元素12, ...},{元素21,元素22 , ...} , ... } 绊谭;
int[ ][ ] a = new int[ ][ ] {{1 , 2 , 3} ,{ 4 , 5},{ 6, 7, 8, 9 } } ;
(3)普通初始化
int[ ][ ] a = new a[3][ ];
a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[4];
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
a[2][0] = 5;
a[2][1] = 6;
(4)使用循環(huán)初始化
int[ ][ ] a = new int[ 5 ][ 6 ] ;
for (int i = 0 ; i < a.length ; i ++) { // a.length:表示數(shù)組行的長度
for (int j = 0; j < a[i].length; j++) { // a[i].length:表示數(shù)組列的長度
a[i][j] = i + j ;
}
}
// 二維數(shù)組的輸出
for (int i = 0 ; i < a.length ; i ++) {
System.out.println(Arrays.toString(a[i])); //a[ i ]:每次循環(huán)輸出一維數(shù)組
}
4、二維數(shù)組的內(nèi)存表示
數(shù)組操作
1汪拥、數(shù)組的遍歷
利用循環(huán)語句和數(shù)組下標(biāo)达传。
int a[10] = {1,2,3,4,5,6,7,8,9,10}
for (int i = 0; i < a.length; i++) {
System.out.print(arr[i]+" ");
}
//輸出結(jié)果:1 2 3 4 5 6 7 8 9 10
2、數(shù)組的復(fù)制 — System.arraycopy()
System.arraycopy ( src, srcPos, dest, destPos, length ) ;
復(fù)制 src 數(shù)組中從下標(biāo) srcPos 開始的 length 個元素到目標(biāo)數(shù)組 dest迫筑,并從目標(biāo)數(shù)組的下標(biāo)為 destPos 的位置開始復(fù)制存儲宪赶。
(1)src :源數(shù)組
(2)srcPos:源數(shù)組的起始位置
(3)dest:目標(biāo)數(shù)組
(4)destPos:目標(biāo)數(shù)組中的起始位置
(5)length:要復(fù)制的源數(shù)組的元素個數(shù)
//數(shù)組copy的方法
int array1[] = {11,23,4,5,6,22,34,67,56,54,54,2};
int array2[] = new int[array1.length];
System.out.println(array1.length);
System.out.println("copy 前 " + Arrays.toString(array2));
System.arraycopy(array1, 0, array2, 1, 6);
System.out.println("copy 后 " + Arrays.toString(array2));
//輸出結(jié)果:
12
copy 前 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
copy 后 [0, 11, 23, 4, 5, 6, 22, 0, 0, 0, 0, 0]
3、數(shù)組的排序
(1)Arrays.sort( arr_name );
對數(shù)組 arr_name 進(jìn)行升序排序脯燃。
(2)Arrays.sort( arr_name逊朽,fromIndex , toIndex);
對數(shù)組 arr_name 中,從下標(biāo)為 fromIndex 到 toIndex 的元素(不包含toIndex )進(jìn)行升序排序曲伊。
int[] a = {1,6,8,5,3,9,4,7,2};
java.util.Arrays.sort(a);
System.out.println(Arrays.toString(a));
//輸出結(jié)果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
nt[] b = {1,6,8,5,3,9,4,7,2};
java.util.Arrays.sort( b , 1 , 5 );
System.out.println(Arrays.toString(b));
//輸出結(jié)果:[1, 3, 5, 6, 8, 9, 4, 7, 2]
4叽讳、數(shù)組的擴(kuò)容
數(shù)據(jù)的擴(kuò)容:實際上是創(chuàng)建了一個新的數(shù)組,將原來數(shù)組的數(shù)據(jù)復(fù)制過來坟募。
Arrays.copyOf(原數(shù)組, 原數(shù)組長度+1);
int[] a = {1,2,3,4,5};
a = Arrays.copyOf(a, a.length+1); //a.length:原數(shù)組長度
a[a.length-1] = 6; //a[a.length-1]:表示新數(shù)組的最后一位
System.out.println(Arrays.toString(a));
//輸出結(jié)果:[1, 2, 3, 4, 5, 6]