Java 學(xué)習(xí)筆記day5_數(shù)組

<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ù)組

  1. 一維數(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>

  1. 如何調(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>

  1. 如何獲取數(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>

  1. 如何遍歷數(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>

  1. 數(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>

  1. 數(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ù)組

  1. 二維數(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>

  1. 如何調(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>

  1. 獲取數(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>

  1. 遍歷二維數(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>

  1. 數(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ù)組

  1. 一維數(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>

  1. 如何調(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>

  1. 如何獲取數(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>

  1. 如何遍歷數(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>

  1. 數(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>

  1. 數(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ù)組

  1. 二維數(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>

  1. 如何調(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>

  1. 獲取數(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>

  1. 遍歷二維數(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>

  1. 數(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>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末些己,一起剝皮案震驚了整個濱河市豌鸡,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌段标,老刑警劉巖涯冠,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異逼庞,居然都是意外死亡蛇更,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進(jìn)店門赛糟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來派任,“玉大人,你說我怎么就攤上這事虑灰《窒梗” “怎么了?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵穆咐,是天一觀的道長颤诀。 經(jīng)常有香客問我,道長对湃,這世上最難降的妖魔是什么崖叫? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮拍柒,結(jié)果婚禮上心傀,老公的妹妹穿的比我還像新娘。我一直安慰自己拆讯,他們只是感情好脂男,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著种呐,像睡著了一般宰翅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上爽室,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天汁讼,我揣著相機(jī)與錄音,去河邊找鬼。 笑死嘿架,一個胖子當(dāng)著我的面吹牛瓶珊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播耸彪,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼伞芹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搜囱?” 一聲冷哼從身側(cè)響起丑瞧,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蜀肘,沒想到半個月后绊汹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扮宠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年西乖,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坛增。...
    茶點(diǎn)故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡获雕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出收捣,到底是詐尸還是另有隱情届案,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布罢艾,位于F島的核電站楣颠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏咐蚯。R本人自食惡果不足惜童漩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望春锋。 院中可真熱鬧矫膨,春花似錦、人聲如沸期奔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呐萌。三九已至馁痴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間搁胆,已是汗流浹背弥搞。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留渠旁,地道東北人攀例。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像顾腊,于是被迫代替她去往敵國和親粤铭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容