數(shù)組 - Array

數(shù)組的基本概念

1、數(shù)組是由多個(gè)數(shù)據(jù)類型相同的元素組成的有順序的數(shù)據(jù)集合。
2、數(shù)組是屬于引用數(shù)據(jù)類型(復(fù)雜數(shù)據(jù)類型)。
3、數(shù)組中的每一個(gè)數(shù)據(jù)叫做數(shù)組的一個(gè)元素(element)。
4、數(shù)組中的元素可以是任何數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型。
5、數(shù)組是固定的,不能擴(kuò)展。
6、數(shù)組中元素的個(gè)數(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)建可以同時(shí)進(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ù)組是存儲(chǔ)多個(gè)相同數(shù)據(jù)類型變量的對象。數(shù)組的所有元素都保存在堆內(nèi)存中。
(2)創(chuàng)建一個(gè)數(shù)組就是在堆中創(chuàng)建一個(gè)數(shù)組對象。
(3)數(shù)組創(chuàng)建后立即擁有默認(rèn)值
(4)數(shù)組的索引是從 0 開始。
(5)數(shù)組在堆中是連續(xù)分配內(nèi)存。


5、數(shù)組的初始化

(1)在創(chuàng)建數(shù)組的同時(shí)就為數(shù)組元素分配內(nèi)存空間并賦值。
(2)如果數(shù)組類型是 int 型,所有元素自動(dòng)初始化為 0 。
(3)如果數(shù)組類型是 boolean型,所有元素自動(dòng)初始化為 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 ][  ] ; 

!注:必須聲明行的個(gè)數(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 個(gè)元素到目標(biāo)數(shù)組 dest,并從目標(biāo)數(shù)組的下標(biāo)為 destPos 的位置開始復(fù)制存儲(chǔ)。
(1)src :源數(shù)組
(2)srcPos:源數(shù)組的起始位置
(3)dest:目標(biāo)數(shù)組
(4)destPos:目標(biāo)數(shù)組中的起始位置
(5)length:要復(fù)制的源數(shù)組的元素個(gè)數(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ò)容:實(shí)際上是創(chuàng)建了一個(gè)新的數(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]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容