<mark style="box-sizing: border-box; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">day5</mark>
Eclipse的使用配置
https://www.eclipse.org/downloads/
<mark style="box-sizing: border-box; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">day5</mark>
Eclipse的使用配置
https://www.eclipse.org/downloads/
數(shù)組
數(shù)組(Array)是多個相同類型數(shù)據(jù)按一定順序排列的集合,并使用一個名字命名遇汞,并通過編號的方式對這些數(shù)據(jù)進(jìn)行統(tǒng)一管理未妹。
常見概念:數(shù)組名、角標(biāo)空入、下標(biāo)络它、索引、元素执庐、數(shù)組長度
數(shù)組特點(diǎn):
數(shù)組是有序排列的
數(shù)組屬于引用數(shù)據(jù)類型的變量酪耕。數(shù)組的元素既可以是基本數(shù)據(jù)類型,也可以是引用數(shù)據(jù)類型
創(chuàng)建數(shù)組對象會在內(nèi)存中開辟一整塊連續(xù)空間
數(shù)組的長度一旦確定就不能修改
數(shù)組分類:
按照維度:一維數(shù)組轨淌、二維數(shù)組……
按照數(shù)組元素類型:基本數(shù)據(jù)類型元素的數(shù)組迂烁、引用數(shù)據(jù)類型元素的數(shù)組
一維數(shù)組
-
一維數(shù)組的聲明和初始化
- 數(shù)組一旦初始化完成,其長度就確定了
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public Static void main(String[] args){
int[] id;
//靜態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作同時進(jìn)行
id = new int[]{1,2,3,4,5};
//int[] id = {1,2,3,4,5};
//動態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作分開進(jìn)行
String[] name = new String[3];
}</pre>
-
如何調(diào)用數(shù)組的指定位置的元素
- 數(shù)組的角標(biāo)(或索引)從 0 開始递鹉,到數(shù)組長度的 -1 結(jié)束
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> name[0] = "張三";
name[1] = "里斯";
name[2] = "汪蕪";</pre>
-
如何獲取數(shù)組的長度
- 屬性:
length
- 屬性:
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> System.out.println(name.length);//3
System.out.println(id.length);//5</pre>
- 如何遍歷數(shù)組
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> for(int i = 0;i < name.length;i++){
System.out.println(names[i]);
}</pre>
-
數(shù)組元素的默認(rèn)初始化值
數(shù)組元素是整型:0
浮點(diǎn)型:0.0
char
型:0 或/u0000
而非‘0’
boolean
型:false
引用數(shù)據(jù)類型:
null
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n198" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest{
public static void main(String[] args){
int[] arr = new int[4];
for(int i = 0;i <arr.length;i++{
System.out.println(arr[i]);
}
short[] arr1 = new short[4];
for(int i = 0;i <arr1.length;i++{
System.out.println(arr1[i]);
}
char[] arr2 = new char[4];
for(int i = 0;i <arr2.length;i++{
System.out.println(arr2[i]);
}
if(arr2[0] == 0){
System.out.println("right");
}
boolean[] arr3 = new boolean[4];
for(int i = 0;i <arr3.length;i++{
System.out.println(arr3[i]);
}
String[] arr4 = new String[4];
System.out.println(arr[2]);
}
}
}
</pre>
-
數(shù)組的內(nèi)存解析
棧(stack)
堆(heap)
方法區(qū):常量池盟步、靜態(tài)域
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //練習(xí)題1:
//升景坊單間短期出租4個月,550元/月(水電煤公攤躏结,網(wǎng)費(fèi)35元/月),空調(diào)却盘、衛(wèi)生間、廚房齊全媳拴。屋內(nèi)均是IT行業(yè)人士黄橘,喜歡安靜。所以要求來租者最好是同行或者剛畢業(yè)的年輕人屈溉,愛干凈塞关、安靜
public class Array Test {
public static void main(String] args){
int[] arr = new int[] {8, 2, 1,0, 3};
int[] index=new int[]{2,0,3,2,4,0,1,3,2,3,3};
String tel = "";
for(int i = 0;i < index.length;i++){
tel += arr[index[i]];
}
System.out.println("聯(lián)系方式:"+tel);
}
}</pre>
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //練習(xí)題2:
import java.util.Scanner;
//ctrl + shift + O:自動導(dǎo)包
/*
- 從鍵盤讀入學(xué)生成績,找出最高分子巾,并輸出學(xué)生成績等級
- 成績>=最高分-10 等級為‘A’
- 成績>=最高分-10 等級為‘A’
- 成績>=最高分-10 等級為‘A’
- 其余 等級為‘D’
- 提示:先讀入學(xué)生人數(shù)帆赢,根據(jù)人數(shù)創(chuàng)建int數(shù)組,存放學(xué)生成績
*/
public class Array Test2 {
public static void main(String[] args) {
//使用Scanner讀取學(xué)生人數(shù)
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入學(xué)生人數(shù):");
//shift + enter:當(dāng)前行內(nèi)任意位置回車到下一行
//ctrl + shift + enter:當(dāng)前行內(nèi)任意位置回車到上一行
//ctrl + D 刪除當(dāng)前行
int number = scanner.nextInt();
//創(chuàng)建數(shù)組线梗,存儲學(xué)生成績椰于,動態(tài)初始化
int[] scores = new int[number];
//給數(shù)組元素賦值
System.out.println("請輸入" + number + "個學(xué)生成績");
for(int i = 0;i < scores.length;i++){
scores[i] = scanner.nextInt();
}
//獲取數(shù)組中的元素最大值
int maxScore = 0;
for(int i = 0;i < scores.length;i++){
if(maxScore < scores[i]) {
maxScore = scores[i];
}
}
//根據(jù)每個學(xué)生成績與最高分的差值,得到每個學(xué)生的等級仪搔,并輸出等級和成績
char level;
for(int i = 0;i < scores.length;i++){
if(maxScore - scores[i] <= 10){
level = 'A';
}else if(maxScore - scores[i] <= 10){
level = 'B';
}else if(maxScore - scores[i] <= 20){
level = 'C';
}else{
level = 'D';
}
System.out.println("student " + i + " score is" + scores[i] + ",grade is" + level);
}
}
}
</pre>
多維數(shù)組
如果說可以把一維數(shù)組當(dāng)成幾何中的線性圖形瘾婿,那么二維數(shù)組就相當(dāng)于一個雙表頭表格
對于二維數(shù)組的理解,我們可以看成是一維數(shù)組 array1 又作為另一個一維數(shù)組 array2 的元素而存在。從數(shù)組的底層運(yùn)行機(jī)制來看憋他,其實(shí)沒有多維數(shù)組
- 二維數(shù)組的聲明和初始化
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest2{
public static void main(String[] args){
//一維數(shù)組:
int[] arr = new int[]{1,2,3};
//二維數(shù)組:
//靜態(tài)初始化:
int[][]arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}}
//動態(tài)初始化1:
String[][]arr2 = new String[3][2];
//動態(tài)初始化2:
String[][]arr3 = new String[3][];
}
}</pre>
- 如何調(diào)用數(shù)組的指定位置的元素
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n224" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1[0][1]);//第一組第二個:2
System.out.println(arr1[2][2]);//第三組第三個:8</pre>
- 獲取數(shù)組長度
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n228" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1.length);//3
System.out.println(arr1[0].length);//3</pre>
- 遍歷二維數(shù)組
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n232" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for(int i = 0;i < arr1.length;i++){
for(int j = 0;j <arr1[i].length;j++){
System.out.println(arr1[i][j] + " ");
}
System.out.println();
}</pre>
-
數(shù)組元素的默認(rèn)初始化值
二維數(shù)組分為外層數(shù)組的元素孩饼、內(nèi)層數(shù)組的元素
int[][] arr = new int[4][3];
外層元素:
arr[0]
,arr[1]
內(nèi)層元素:
arr[0][0]
竹挡,arr[1][2]
等
針對初始化方式一:
int[][] arr = new int[4][3];
外層元素的初始化值為:地址值
內(nèi)層元素的初始化值為:與一維數(shù)組的初始化情況相同
針對初始化方式二:
int[][] arrr = new int[4][];
外層元素的初始化值為:
null
內(nèi)層元素的初始化值為:不能調(diào)用镀娶,否則報錯
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n256" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class ArrayTest2 {
public static void main(String[] args) {
int[][] arr1 = new int[3][2];
System.out.println(arr1[0]);//地址值:[I@182decdb
System.out.println(arr1[0][0]);//0
System.out.println(arr1);//二維地址值
String[][] arr2 = new String[4][2];
System.out.println(arr2[1]);//地址值
System.out.println(arr2[1][1]);//null
String[][] arr3 = new String[4][];
System.out.println(arr3[1]);//null
System.out.println(arr3[1][1]);//空指針異常
}
}
</pre>
數(shù)組
數(shù)組(Array)是多個相同類型數(shù)據(jù)按一定順序排列的集合,并使用一個名字命名揪罕,并通過編號的方式對這些數(shù)據(jù)進(jìn)行統(tǒng)一管理梯码。
常見概念:數(shù)組名、角標(biāo)好啰、下標(biāo)轩娶、索引、元素框往、數(shù)組長度
數(shù)組特點(diǎn):
數(shù)組是有序排列的
數(shù)組屬于引用數(shù)據(jù)類型的變量鳄抒。數(shù)組的元素既可以是基本數(shù)據(jù)類型,也可以是引用數(shù)據(jù)類型
創(chuàng)建數(shù)組對象會在內(nèi)存中開辟一整塊連續(xù)空間
數(shù)組的長度一旦確定就不能修改
數(shù)組分類:
按照維度:一維數(shù)組椰弊、二維數(shù)組……
按照數(shù)組元素類型:基本數(shù)據(jù)類型元素的數(shù)組许溅、引用數(shù)據(jù)類型元素的數(shù)組
一維數(shù)組
-
一維數(shù)組的聲明和初始化
- 數(shù)組一旦初始化完成,其長度就確定了
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public Static void main(String[] args){
int[] id;
//靜態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作同時進(jìn)行
id = new int[]{1,2,3,4,5};
//int[] id = {1,2,3,4,5};
//動態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作分開進(jìn)行
String[] name = new String[3];
}</pre>
-
如何調(diào)用數(shù)組的指定位置的元素
- 數(shù)組的角標(biāo)(或索引)從 0 開始秉版,到數(shù)組長度的 -1 結(jié)束
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> name[0] = "張三";
name[1] = "里斯";
name[2] = "汪蕪";</pre>
-
如何獲取數(shù)組的長度
- 屬性:
length
- 屬性:
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> System.out.println(name.length);//3
System.out.println(id.length);//5</pre>
- 如何遍歷數(shù)組
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> for(int i = 0;i < name.length;i++){
System.out.println(names[i]);
}</pre>
-
數(shù)組元素的默認(rèn)初始化值
數(shù)組元素是整型:0
浮點(diǎn)型:0.0
char
型:0 或/u0000
而非‘0’
boolean
型:false
引用數(shù)據(jù)類型:
null
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n198" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest{
public static void main(String[] args){
int[] arr = new int[4];
for(int i = 0;i <arr.length;i++{
System.out.println(arr[i]);
}
short[] arr1 = new short[4];
for(int i = 0;i <arr1.length;i++{
System.out.println(arr1[i]);
}
char[] arr2 = new char[4];
for(int i = 0;i <arr2.length;i++{
System.out.println(arr2[i]);
}
if(arr2[0] == 0){
System.out.println("right");
}
boolean[] arr3 = new boolean[4];
for(int i = 0;i <arr3.length;i++{
System.out.println(arr3[i]);
}
String[] arr4 = new String[4];
System.out.println(arr[2]);
}
}
}
</pre>
-
數(shù)組的內(nèi)存解析
棧(stack)
堆(heap)
方法區(qū):常量池贤重、靜態(tài)域
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //練習(xí)題1:
//升景坊單間短期出租4個月,550元/月(水電煤公攤清焕,網(wǎng)費(fèi)35元/月),空調(diào)并蝗、衛(wèi)生間、廚房齊全秸妥。屋內(nèi)均是IT行業(yè)人士滚停,喜歡安靜。所以要求來租者最好是同行或者剛畢業(yè)的年輕人粥惧,愛干凈键畴、安靜
public class Array Test {
public static void main(String] args){
int[] arr = new int[] {8, 2, 1,0, 3};
int[] index=new int[]{2,0,3,2,4,0,1,3,2,3,3};
String tel = "";
for(int i = 0;i < index.length;i++){
tel += arr[index[i]];
}
System.out.println("聯(lián)系方式:"+tel);
}
}</pre>
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //練習(xí)題2:
import java.util.Scanner;
//ctrl + shift + O:自動導(dǎo)包
/*
- 從鍵盤讀入學(xué)生成績,找出最高分影晓,并輸出學(xué)生成績等級
- 成績>=最高分-10 等級為‘A’
- 成績>=最高分-10 等級為‘A’
- 成績>=最高分-10 等級為‘A’
- 其余 等級為‘D’
- 提示:先讀入學(xué)生人數(shù),根據(jù)人數(shù)創(chuàng)建int數(shù)組檩禾,存放學(xué)生成績
*/
public class Array Test2 {
public static void main(String[] args) {
//使用Scanner讀取學(xué)生人數(shù)
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入學(xué)生人數(shù):");
//shift + enter:當(dāng)前行內(nèi)任意位置回車到下一行
//ctrl + shift + enter:當(dāng)前行內(nèi)任意位置回車到上一行
//ctrl + D 刪除當(dāng)前行
int number = scanner.nextInt();
//創(chuàng)建數(shù)組挂签,存儲學(xué)生成績,動態(tài)初始化
int[] scores = new int[number];
//給數(shù)組元素賦值
System.out.println("請輸入" + number + "個學(xué)生成績");
for(int i = 0;i < scores.length;i++){
scores[i] = scanner.nextInt();
}
//獲取數(shù)組中的元素最大值
int maxScore = 0;
for(int i = 0;i < scores.length;i++){
if(maxScore < scores[i]) {
maxScore = scores[i];
}
}
//根據(jù)每個學(xué)生成績與最高分的差值盼产,得到每個學(xué)生的等級饵婆,并輸出等級和成績
char level;
for(int i = 0;i < scores.length;i++){
if(maxScore - scores[i] <= 10){
level = 'A';
}else if(maxScore - scores[i] <= 10){
level = 'B';
}else if(maxScore - scores[i] <= 20){
level = 'C';
}else{
level = 'D';
}
System.out.println("student " + i + " score is" + scores[i] + ",grade is" + level);
}
}
}
</pre>
多維數(shù)組
如果說可以把一維數(shù)組當(dāng)成幾何中的線性圖形,那么二維數(shù)組就相當(dāng)于一個雙表頭表格
對于二維數(shù)組的理解戏售,我們可以看成是一維數(shù)組 array1 又作為另一個一維數(shù)組 array2 的元素而存在侨核。從數(shù)組的底層運(yùn)行機(jī)制來看草穆,其實(shí)沒有多維數(shù)組
- 二維數(shù)組的聲明和初始化
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest2{
public static void main(String[] args){
//一維數(shù)組:
int[] arr = new int[]{1,2,3};
//二維數(shù)組:
//靜態(tài)初始化:
int[][]arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}}
//動態(tài)初始化1:
String[][]arr2 = new String[3][2];
//動態(tài)初始化2:
String[][]arr3 = new String[3][];
}
}</pre>
- 如何調(diào)用數(shù)組的指定位置的元素
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n224" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1[0][1]);//第一組第二個:2
System.out.println(arr1[2][2]);//第三組第三個:8</pre>
- 獲取數(shù)組長度
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n228" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1.length);//3
System.out.println(arr1[0].length);//3</pre>
- 遍歷二維數(shù)組
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n232" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for(int i = 0;i < arr1.length;i++){
for(int j = 0;j <arr1[i].length;j++){
System.out.println(arr1[i][j] + " ");
}
System.out.println();
}</pre>
-
數(shù)組元素的默認(rèn)初始化值
二維數(shù)組分為外層數(shù)組的元素、內(nèi)層數(shù)組的元素
int[][] arr = new int[4][3];
外層元素:
arr[0]
搓译,arr[1]
內(nèi)層元素:
arr[0][0]
悲柱,arr[1][2]
等
針對初始化方式一:
int[][] arr = new int[4][3];
外層元素的初始化值為:地址值
內(nèi)層元素的初始化值為:與一維數(shù)組的初始化情況相同
針對初始化方式二:
int[][] arrr = new int[4][];
外層元素的初始化值為:
null
內(nèi)層元素的初始化值為:不能調(diào)用,否則報錯
<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n256" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class ArrayTest2 {
public static void main(String[] args) {
int[][] arr1 = new int[3][2];
System.out.println(arr1[0]);//地址值:[I@182decdb
System.out.println(arr1[0][0]);//0
System.out.println(arr1);//二維地址值
String[][] arr2 = new String[4][2];
System.out.println(arr2[1]);//地址值
System.out.println(arr2[1][1]);//null
String[][] arr3 = new String[4][];
System.out.println(arr3[1]);//null
System.out.println(arr3[1][1]);//空指針異常
}
}
</pre>