數組:是一組相關變量的集合
數組是一組相關數據的集合,一個數組實際上就是一連串的變量,數組按照使用可以分為一維數組、二維數組、多維數組
數據的有點
不使用數組定義100個整形變量:int i1;int i2;int i3
使用數組定義 int i[100];
數組定義:int i[100];只是一個偽代碼,只是表示含義的
一維數組
一維數組可以存放上千萬個數據,并且這些數據的類型是完全相同的,
使用java數組,必須經過兩個步驟,聲明數組和分配內存給該數組,
聲明形式一
聲明一維數組:數據類型 數組名[]=null;
非配內存給數組:數組名=new 數據類型[長度];
聲明形式二
聲明一維數組:數據類型 [] 數組名=null;
java數據類型分為兩大類
基本數據類型
int、long操作的時候本身就是具體的內容
引用數據類型:數組、類、接口
引用傳遞的就是一個內存的使用權,一塊內存空間,可能有多個人同時使用
事例聲明數組
1
2
3
4
5
6
7
8
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]= null ; //聲明數組 score= new int [ 3 ]; //開辟空間,大小為3 } } |
數組的聲明格式里,數據類型是數組元素的數據類型,常見的有整形、浮點型、與字符型等
數組名是用來統一這組相同數據類型元素的名稱,其命名規則和變量的相同
數組聲明后實際上是在棧內存中保存了此數組的名稱,結下了是要在堆內存中配置數組所需要的內存,齊產固定是告訴編譯器,所聲明的數組要存放多少個元素,而new 則是命令編譯器根據括號里的長度
基本數據類型偶讀有其默認值:int 0;只要是引用數據類型默認值就是null
事例
1
2
3
4
5
6
7
8
9
10
11
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]= null ; //聲明數組 score= new int [ 3 ]; //開辟空間,大小為3 System.out.print( "score[0]=" +score[ 0 ]); System.out.print( "\tscore[1]=" +score[ 1 ]); System.out.print( "\tscore[2]=" +score[ 2 ]); } } |
堆棧內存的解釋
數組操作中,在棧內存中保存的永遠是數組的名稱,只開辟了棧內的空間,數組是永遠無法使用的,必須有指向的對內存才可以使用,要想開辟新對內存空間必須使用new關鍵字,之后就是將對內存的使用權交給對應的棧內存,而且一個堆內存空間可以同時被多個棧內存空間指向,比如一個人可以有多個名字,人就相當于對內存,名字就相當于棧內存
聲明數組的同時分配內存空間
聲明數組的同時非配內存
數據類型 數組名[]=new 數據類型[個數]
int score[]=new int[10];
聲明一個元素個數為10的整形數組score,同時開辟依靠內存空間工期使用
java中,由于整形數據類型占用的空間為4個byte,而整個數組score可保存的元素有10個。所以上例中占用的內存共有4*10=40個字節
數組的訪問
數組中元素的表示方法
想要訪問數組里的元素可以利用索引來完成,java的數組索引標號由10開始,以一個score[10]的整形數組為例,score[0]代表第一個元素
一直向下,最后一個為score[9]
取得數組的長度
在java中取得數組的長度(也就是數組元素的長度)可以利用數組名稱.length完成,
數組名稱.length--返回一個int類型的數據
1
2
3
4
5
6
7
8
9
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]= null ; //聲明數組 score= new int [ 3 ]; //開辟空間,大小為3 System.out.println(score.length); } } |
結果
數組的靜態初始化
之前的數組,所采用的都是動態初始化,所有的內容在數組聲明的時候并不具體的指定,而是以默認值的形式出現
靜態初始化是指在數組聲明后直接為數組指定具體的內容
如果想要直接在聲明的時候給數組賦初始值,可以采用大括號完成,只要在數組的生命格式后面加上初值的賦值即可,
數據類型 數組名 []={初始值0,初始值1,初始值3,....初始值n};
1
2
3
4
5
6
7
8
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]={ 1 , 2 , 3 , 4 , 5 , 6 }; //使用靜態初始化聲明數組 System.out.println(score.length); } } |
結果
6
范例求出數組中的最大值和最小值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]={ 45 , 12 , 8 , 23 , 89 , 56 }; //使用靜態初始化聲明數組 int max= 0 ; int min= 0 ; max=min=score[ 0 ]; for ( int i = 0 ; i < score.length; i++) { if (score[i]>max){ max=score[i]; } if (score[i] min=score[i]; } } System.out.println( "最大值:" +max); System.out.println( "最小值:" +min); } } |
結果
范例排序,在操作中排序是比較常用的
從大到小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.qn.array; public class Test { public static void main(String[] args) { int score[]={ 45 , 12 , 8 , 23 , 89 , 56 }; //使用靜態初始化聲明數組 int temp= 0 ; for ( int i = 0 ; i < score.length; i++) { for ( int j = 0 ; j < score.length- 1 ; j++) { if (score[i]>score[j]){ temp=score[i]; score[i]=score[j]; score[j]=temp; } } } for ( int i = 0 ; i < score.length; i++) { System.out.print(score[i]+ "\t" ); } } } |
結果
這個時候不要被i值所迷惑 if(score[i]>score[j]){
這一步主要知識為了比較,實際上完成之后輸出的時候是根據j的值排序的
二維數組
如果可以把一維數組當做幾何中的一條線圖形,那么二維數組就相當于一個表格
A B
1 姓名 年齡
2 齊寧 21
3 齊燕 23
4 齊威 26
二維數組聲明的方式和以為數組的類似,內存分配也一樣是用new這個關鍵字
其實聲明與分配內存的格式如下
動態初始化
數據類型 數組名[][];
數組名=new 數據類型[行的個數][列的個數];
聲明并初始化數組
數據類型 數組名[][]=new 數據類型[行的個數][列的個數];
靜態初始化
二維數組的存儲
聲明二維數組score 同時開辟一段內存空間
int score[][]=new int[4][3];
整體數據score可保存的元素是4*3=12個,在java中,int數據類型所占用的空間為4個字節,因此該整形數組占用的內存共為4*12=48個字節
事例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.qn.array; public class test1 { public static void main(String[] args) { int score[][] = new int [ 4 ][ 3 ]; score[ 0 ][ 1 ] = 30 ; score[ 1 ][ 0 ] = 31 ; score[ 2 ][ 1 ] = 32 ; score[ 2 ][ 2 ] = 33 ; score[ 3 ][ 1 ] = 34 ; score[ 1 ][ 1 ] = 35 ; for ( int i = 0 ; i < score.length; i++) { for ( int j = 0 ; j < score[i].length; j++) { System.out.print(score[i][j]+ "\t" ); } System.out.println( "" ); } } } |
結果
二維數組靜態初始化
用到的時候才會開辟空間,不用的(紅色部分)則不開辟空間
多維數組
一般只是用到二維數組
三維數組簡單了解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.qn.array; public class test1 { public static void main(String[] args) { int score[][][]={ { { 5 , 1 },{ 6 , 7 } }, { { 9 , 4 },{ 8 , 3 } } }; System.out.print(score[ 0 ][ 0 ][ 0 ]+ "\t" ); System.out.print(score[ 0 ][ 0 ][ 1 ]+ "\t" ); System.out.print(score[ 0 ][ 0 ][ 0 ]+ "\t" ); System.out.print(score[ 0 ][ 0 ][ 1 ]+ "\t" ); System.out.print(score[ 1 ][ 0 ][ 0 ]+ "\t" ); System.out.print(score[ 1 ][ 0 ][ 1 ]+ "\t" ); System.out.print(score[ 1 ][ 1 ][ 0 ]+ "\t" ); System.out.print(score[ 1 ][ 1 ][ 1 ]+ "\t" ); } } |
以上這篇java中數組的定義及使用方法(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。