Java基礎編程及其思維導圖
目錄:
Java學習導圖
一、Java基本語法
1.關鍵字與標識符 2.變量分類 3.運算符 4.流程控制
二、數組
1.數組概述 2.一維數組 3.二維數組 4.數組常見算法 5.Arrays工具類使用
三、面向對象
Java類及其類成員三大特性(封裝、繼承、多態)關鍵字
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
Java學習思維導圖
一、Java基本語法
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
Java基本語法
1.關鍵字與標識符
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
關鍵字與標識符
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
2.變量分類
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
定義變量格式
變量類型 變量名 = 變量值;
變量類型 變量名;變量名 = 變量值;
變量使用注意點
① 變量必須先聲明,後使用
② 變量都定義在其作用域內。在作用域內,它是有效的。換句話說,出了作用域,就失效了
③ 同一個作用域內,不可以聲明兩個同名的變量
基本數據變量運算規則
自動類型轉換:結論:當容量小的數據類型的變量與容量大的數據類型的變量做運算時,結果自動提升爲容量大的數據類型。byte 、char 、short --> int --> long --> float --> double特別的:當byte、char、short三種類型的變量做運算時,結果爲int型
說明:此時的容量大小指的是,表示數的範圍的大和小。比如:float容量要大於long的容量
強制類型轉換:
1.需要使用強轉符:()
2.注意點:強制類型轉換,可能導致精度損失。
3.運算符
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
算術運算符: + - + - * / % (前)++ (後)++ (前)-- (後)-- +
特別說明:
1.(前)++ :先自增1,後運算
(後)++ :先運算,後自增1
2.(前)-- :先自減1,後運算
(後)-- :先運算,後自減1
3.連接符:+:只能使用在String與其他數據類型變量之間使用。
複製運算符:= += -= *= /= %=
特別說明:
1.運算的結果不會改變變量本身的數據類型
2.
開發中,如果希望變量實現+1的操作,有幾種方法?(前提:int num = 10;)
//方式一:num = num + 1;
//方式二:num += 1;
//方式三:num++; (推薦)
比較運算符:== != > < >= <= instanceof
特別說明:
1.比較運算符的結果是boolean類型
2.> < >= <= :只能使用在數值類型的數據之間。
3. == 和 !=: 不僅可以使用在數值類型數據之間,還可以使用在其他引用類型變量之間。
邏輯運算符:& && | || ! ^
特別說明的:
1.邏輯運算符操作的都是boolean類型的變量。而且結果也是boolean類型
2.區分& 與 &&
相同點1:& 與 && 的運算結果相同
相同點2:當符號左邊是true時,二者都會執行符號右邊的運算
不同點:當符號左邊是false時,&繼續執行符號右邊的運算。&&不再執行符號右邊的運算。
開發中,推薦使用&&
3.區分:| 與 ||
相同點1:| 與 || 的運算結果相同
相同點2:當符號左邊是false時,二者都會執行符號右邊的運算
不同點3:當符號左邊是true時,|繼續執行符號右邊的運算,而||不再執行符號右邊的運算
開發中,推薦使用||
位運算符:<< >> >>> & | ^ ~
特別說明:
位運算符操作的都是整型的數據
<<:在一定範圍內,每向左移1位,相當於 * 2
>>: 在一定範圍內,每向右移1位,相當於/2
三元運算符:(條件表達式)? 表達式1 : 表達式2
特別說明
說明
① 條件表達式的結果爲boolean類型
② 根據條件表達式真或假,決定執行表達式1,還是表達式2.
如果表達式爲true,則執行表達式1。
如果表達式爲false,則執行表達式2。
③ 表達式1 和表達式2要求是一致的。
④ 三元運算符可以嵌套使用
凡是可以使用三元運算符的地方,都可以改寫爲if-else
反之,不成立。
如果程序既可以使用三元運算符,又可以使用if-else結構,那麼優先選擇三元運算符。原因:簡潔、執行效率高。
4.流程控制
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
分支結構:
1.if-else條件判斷結構
結構一:
if (條件表達式) {
執行表達式
}
結構二:二選一
if (條件表達式) {
執行表達式1
}else{
執行表達式2
}
結構三:n選一
if (條件表達式) {
執行表達式1
}else if (條件表達式) {
執行表達式2
}else if (條件表達式) {
執行表達式3
}
…
else{
執行表達式n
}
2.switch-case選擇結構
switch (表達式) {
case 常量1:
執行語句1;
break;
case 常量2:
執行語句2;
break;
…
default:
執行語句n;
break;
}
循環結構:
1.循環結構的四要素
① 初始化條件
② 循環條件 —>是boolean類型
③ 循環體
④ 迭代條件
說明:通常情況下,循環結束都是因爲②中循環條件返回false了。
2.三種循環結構:
2.1 for循環結構
for(①;②;④){
③
}
執行過程:① - ② - ③ - ④ - ② - ③ - ④ - … - ②
2.2 while循環結構
①
while(②){
③;
④;
}
執行過程:① - ② - ③ - ④ - ② - ③ - ④ - … - ②
說明:
寫while循環千萬小心不要丟了迭代條件。一旦丟了,就可能導致死循環!
for和while循環總結:
開發中,基本上我們都會從for、while中進行選擇,實現循環結構。for循環和while循環是可以相互轉換的!
區別:for循環和while循環的初始化條件部分的作用範圍不同我們寫程序,要避免出現死循環。
2.3 do-while循環結構
①
do{
③;
④;
}while(②);
執行過程:① - ③ - ④ - ② - ③ - ④ - … - ②
說明:
1.do-while循環至少會執行一次循環體!
2.開發中,使用for和while更多一些。較少使用do-while
二、數組
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
1.數組概述
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
1.數組理解:數組(Array),是多個相同類型數據一定順序排列的集合,並使用一個名字命名,並通過編號的方式對這些數據進行統一管理。
2.數組相關的概念:
數組名
元素
角標、下標、索引
數組的長度:元素的個數
3.數組的特點:
數組是序排列的
數組屬於引用數據類型的變量。數組的元素,既可以是基本數據類型,也可以是引用數據類型
創建數組對象會在內存中開闢一整塊連續的空間
數組的長度一旦確定,就不能修改。
4. 數組的分類:
① 二維數:一維數組、二維數組、。。。
② 照數組元素的類型:基本數據類型元素的數組、引用數據類型元素的數組
2.一維數組
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
1.一維數組的聲明與初始化
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] ids;<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span>聲明<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">//<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1.1</span> 靜態初始化:數組的初始化和數組元素的賦值操作同時進行<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">ids = new <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[]{<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1001</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1002</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1003</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1004</span>};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1.2</span>動態初始化:數組的初始化和數組元素的賦值操作分開進行<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">String[] names = new String[<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">5</span>];<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"> <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] arr4 = {<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">2</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">3</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">4</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">5</span>};<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span>類型推斷<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1234567</span></code>
錯誤的方式:
// int[] arr1 = new int[];
// int[5] arr2 = new int[5];
// int[] arr3 = new int[3]{1,2,3};
2.一維數組元素的引用:通過角標的方式調用。
數組的角標(或索引從0開始的,到數組的長度-1結束)
3.數組的屬性:length
System.out.println(names.length);//5
System.out.println(ids.length);
說明:
數組一旦初始化,其長度就是確定的。arr.length
數組長度一旦確定,就不可修改。
4.一維數組的遍歷
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}
5.一維數組元素的默認初始化值
> 數組元素是整型:0
> 數組元素是浮點型:0.0
> 數組元素是char型:0或’\u0000’,而非’0’
> 數組元素是boolean型:false
> 數組元素是引用數據類型:null
3.二維數組
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
1.如何理解二維數組?
數組屬於引用數據類型
數組的元素也可以是引用數據類型
一個一維數組A的元素如果還是一個一維數組類型的,則,此數組A稱爲二維數組。
2.二維數組的聲明與初始化
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;">int[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>] arr = new int[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>]{1,2,3};//一維數組<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">//靜態初始化int[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);"></span>] arr1 = new int[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);"></span>]{{1,2,3},{4,5},{6,7,8}};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">//動態初始化1String[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);"></span>] arr2 = new String[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">3</span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">2</span>];<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">//動態初始化2String[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);"></span>] arr3 = new String[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">3</span>][<span class="hljs-symbol" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);"></span>];<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">//也是正確的寫法:int[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>] arr4[<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);"></span>] = new int[][]{{1,2,3},{4,5,9,10},{6,7,8}};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">int[] arr5[] = {{1,2,3},{4,5},{6,7,8}};//類型推斷12345678910</code>
錯誤的方式:
// String[][] arr4 = new String[][4];
// String[4][3] arr5 = new String[][];
// int[][] arr6 = new int[4][3]{{1,2,3},{4,5},{6,7,8}};
3.如何調用二維數組元素:
System.out.println(arr1[0][1]);//2
System.out.println(arr2[1][1]);//null
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;">arr3[<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>] = <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">new</span> String[<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">4</span>];<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">out</span>.println(arr3[<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>][<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">0</span>]);<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">out</span>.println(arr3[<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">0</span>]);<span class="hljs-comment" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);">//</span><br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-comment" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);">123</span></code>
4.遍歷二維數組元素
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">for</span>(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span> i = <span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">0</span>; i lt arr.length;i++)<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">for</span>(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span> j = <span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">0</span>;j lt arr[i].length;j++){<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">out</span>.print(arr[i][j] + <span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">" "</span>)<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">}<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1234</span></code>
4.數組常見算法
1.數組的創建與元素賦值:
楊輝三角(二維數組)、回形數(二維數組)、6個數,1-30之間隨機生成且不重複。
2.針對於數值型的數組:
最大值、最小值、總和、平均數等
3.數組的複製與複製
int[] array1,array2;
array1 = new int[]{1,2,3,4};
3.1 賦值:
array2 = array1;
如何理解:將array1保存的數組的地址值賦給了array2,使得array1和array2共同指向堆空間中的同一個數組實體。
3.2 複製:
array2 = new int[array1.length];
for(int i = 0;i < array2.length;i++){
array2[i] = array1[i];
}
如何理解:我們通過new的方式,給array2在堆空間中新開闢了數組的空間。將array1數組中的元素值一個一個的賦值到array2數組中。
5.Arrays工具類使用
1.理解:
① 定義在java.util包下。
② Arrays:提供了很多操作數組的方法。
2.使用:
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;">//<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>.boolean equals(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] a,<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] b):判斷兩個數組是否相等。<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] arr1 = new <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[]{<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">2</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">3</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">4</span>};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] arr2 = new <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[]{<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">3</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">2</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">4</span>};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">boolean isEquals = Arrays.equals(arr1, arr2);System.out.println(isEquals);<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">2</span>.String toString(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] a):輸出數組信息。<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.out.println(Arrays.toString(arr1));<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">3</span>.void fill(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] a,<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span> val):將指定值填充到數組之中。<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">Arrays.fill(arr1,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">10</span>);<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.out.println(Arrays.toString(arr1));<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">4</span>.void <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">sort</span>(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] a):對數組進行排序。<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">Arrays.sort(arr2);System.out.println(Arrays.toString(arr2));<span class="hljs-regexp" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(217, 99, 34);">//</span><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">5</span>.int binarySearch(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] a,<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span> key)<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[] arr3 = new <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span>[]{-<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">98</span>,-<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">34</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">2</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">34</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">54</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">66</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">79</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">105</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">210</span>,<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">333</span>};<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">int</span> <span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">index</span> = Arrays.binarySearch(arr3, <span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">210</span>);<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">if</span>(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">index</span> gt= <span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">0</span>){<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.out.println(<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">index</span>);<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">}<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">else</span>{<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">System.out.println(<span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">"未找到"</span>);<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">}<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">123456789101112131415161718192021222324252627</span></code>
三、面向對象
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
java類及其類成員
1.類的設計中,兩個重要結構之一:屬性
對比:屬性 vs 局部變量
1.相同點:
1.1 定義變量的格式:數據類型 變量名 = 變量值
1.2 先聲明,後使用
1.3 變量都其對應的作用域
2.不同點:
2.1 在類中聲明的位置的不同
屬性:直接定義在類的一對{}內
局部變量:聲明在方法內、方法形參、代碼塊內、構造器形參、構造器內部的變量
2.2 關於權限修飾符的不同
屬性:可以在聲明屬性時,指明其權限,使用權限修飾符。
常用的權限修飾符:private、public、缺省、protected —>封裝性
目前,大家聲明屬性時,都使用缺性就可以了。
局部變量:不可以使用權限修飾符。
2.3 默認初始化值的情況:
屬性:類的屬性,根據其類型,都默認初始化值。
整型(byte、short、int、long:0)
浮點型(float、double:0.0)
字符型(char:0 (或’\u0000’))
布爾型(boolean:false)
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;">引用數據類型(類、數組、接口:<span class="hljs-literal" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(138, 115, 4);">null</span>)<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">局部變量:沒默認初始化值。意味着,我們在調用局部變量之前,一定要顯式賦值。特別地:形參在調用時,我們賦值即可。<span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">12345</span></code>
2.4 在內存中加載的位置:
屬性:加載到堆空間中 (非static)
局部變量:加載到棧空間
2.類的設計中,兩個重要結構之二:方法
方法:描述類應該具的功能。
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;"><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1</span><span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">.舉例:</span><br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-string" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(0, 117, 59);">1</span></code>
public void eat(){}
public void sleep(int hour){}
public String getName(){}
public String getNation(String nation){}
2. 方法的聲明:權限修飾符 返回值類型 方法名(形參列表){
方法體
}
3. 說明:
3.1 關於權限修飾符:默認方法的權限修飾符先都使用public
Java規定的4種權限修飾符:private、public、缺省、protected -->封裝性再細說
3.2 返回值類型: 返回值 vs 沒返回值
3.2.1 如果方法返回值,則必須在方法聲明時,指定返回值的類型。同時,方法中,需要使用 return關鍵字來返回指類型的變量或常量:“return 數據”。如果方法沒返回值,則方法聲明時,使用void來表示。通常,沒返回值的方法中,就不需要使用return.但是,如果使用的話,只能“return;”表示結束此方法的意思。
構造器(或構造方法):
構造器的作用:
1.創建對象
2.初始化對象的信息
使用說明:
1.如果沒顯式的定義類的構造器的話,則系統默認提供一個空參的構造器
2.定義構造器的格式:權限修飾符 類名(形參列表){}
3.一個類中定義的多個構造器,彼此構成重載
4.一旦我們顯式的定義了類的構造器之後,系統就不再提供默認的空參構造器
5.一個類中,至少會有一個構造器。
三大特性(封裝、繼承、多態)
面向對象的特徵一:封裝與隱藏
1.爲什麼要引入封裝性?
我們程序設計追求“高內聚,低耦合”。
高內聚 :類的內部數據操作細節自己完成,不允許外部干涉;
低耦合 :僅對外暴露少量的方法用於使用。
隱藏對象內部的複雜性,只對外公開簡單的接口。便於外界調用,從而提高系統的可擴展性、可維護性。通俗的說,把該隱藏的隱藏起來,該暴露的暴露出來。這就是封裝性的設計思想。
2.封裝性思想具體的代碼體現:
體現一:將類的屬性xxx私化(private),同時,提供公共的(public)方法來獲取(getXxx)和設置(setXxx)此屬性的值
private double radius;
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
體現二:不對外暴露的私有的方法
體現三:單例模式(將構造器私有化)
體現四:如果不希望類在包外被調用,可以將類設置爲缺省的。
3.Java規定的四種權限修飾符
我把Java基礎編程及思維導圖整理的超級詳細,小白都能看懂
3.1 權限從小到大順序爲:private < 缺省 < protected < public
3.2 具體的修飾範圍:
3.3 權限修飾符可用來修飾的結構說明:
4.種權限都可以用來修飾類的內部結構:屬性、方法、構造器、內部類
修飾類的話,只能使用:缺省、public
面向對象特徵二:繼承性
1.繼承性的格式:
class A extends B{}
A:子類、派生類、subclass
B:父類、超類、基類、superclass
2.子類繼承父類以後有哪些不同?
2.1體現:一旦子類A繼承父類B以後,子類A中就獲取了父類B中聲明的所有的屬性和方法。
特別的,父類中聲明爲private的屬性或方法,子類繼承父類以後,仍然認爲獲取了父類中私的結構。只因爲封裝性的影響,使得人類不能直接調用父類的結構而已。
2.2 子類繼承父類以後,還可以聲明自己特有的屬性或方法:實現功能的拓展。
子類和父類的關係,不同於子集和集合的關係。
extends:延展、擴展
3.Java中繼承性的說明
3.1.一個類可以被多個子類繼承。
3.2.Java中類的單繼承性:一個類只能有一個父類
3.3.子父類是相對的概念。
3.4.子類直接繼承的父類,稱爲:直接父類。間接繼承的父類稱爲:間接父類
3.5.子類繼承父類以後,就獲取了直接父類以及所間接父類中聲明的屬性和方法
面向對象的特性三:多態性
1.多態性的理解:可以理解爲一個事物的多種形態。
2.何爲多態性:
對象的多態性:父類的引用指向子類的對象(或子類的對象賦給父類的引用)
舉例:
Person p = new Man();
Object obj = new Date();
3.多態性的使用:虛擬方法調用
有了對象的多態性以後,我們在編譯期,只能調用父類中聲明的方法,但在運行期,我們實際執行的是子類重寫父類的方法。
總結:編譯,看左邊;運行,看右邊。
4.多態性的使用前提:
① 類的繼承關係 ② 方法的重寫
6.多態性使用的注意點:
對象的多態性,只適用於方法,不適用於屬性(編譯和運行都看左邊)
關鍵字
關鍵字:this
1.可以調用的結構:屬性、方法;構造器
2.this調用屬性、方法:
this理解爲:當前對象 或 當前正在創建的對象
2.1 在類的方法中,我們可以使用"this.屬性"或"this.方法"的方式,調用當前對象屬性或方法。但是通常情況下,我們都擇省略"this."。特殊情況下,如果方法的形參和類的屬性同名時,我們必須顯式的使用"this.變量"的方式,表明此變量是屬性,而非形參。
2.2 在類的構造器中,我們可以使用"this.屬性"或"this.方法"的方式,調用當前正在創建的對象屬性或方法。但是,通常情況下,我們都擇省略"this."。特殊情況下,如果構造器的形參和類的屬性同名時,我們必須顯式的使用"this.變量"的方式,表明此變量是屬性,而非形參。
3.this調用構造器:
① 我們在類的構造器中,可以顯式的使用"this(形參列表)"方式,調用本類中指定的其他構造器
② 構造器中不能通過"this(形參列表)“方式調用自己
③ 如果一個類中有n個構造器,則最多有 n - 1構造器中使用了"this(形參列表)”
④ 規定:"this(形參列表)“必須聲明在當前構造器的首行
⑤ 構造器內部,最多隻能聲明一個"this(形參列表)”,用來調用其他的構造器
關鍵字:abstract
abstract: 抽象的
1.可以用來修飾:類、方法
2.具體的:
abstract修飾類:抽象類
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;"> 此類不能實例化<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"> 抽象類中一定有構造器,便於子類實例化時調用(涉及:子類對象實例化的全過程) 開發中,都會提供抽象類的子類,讓子類對象實例化,完成相關的操作 <span class="hljs-comment" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);">---gt抽象的使用前提:繼承性</span><br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-comment" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);">123</span></code>
abstract修飾方法:抽象方法
<code style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 1em; margin: 0px; padding: 0px; border: 0px;">抽象方法只方法的聲明,沒方法體<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;">包含抽象方法的類,一定是一個抽象類。反之,抽象類中可以沒有抽象方法的。 若子類重寫了父類中的所的抽象方法後,此子類方可實例化 若子類沒重寫父類中的所的抽象方法,則此子類也是一個抽象類,需要使用<span class="hljs-keyword" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(17, 75, 166);">abstract</span>修飾<br style="-webkit-tap-highlight-color: transparent; box-sizing: border-box;"><span class="hljs-number" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; color: rgb(168, 46, 46);">1234</span></code>
3.注意點:
1.abstract不能用來修飾:屬性、構造器等結構
2.abstract不能用來修飾私方法、靜態方法、final的方法、final的類
關注作者-私:" Java "
免費領取一份(Java學習視頻,技術文檔,電子書籍,面試等資料...)
還可免費學習Java基礎到項目實戰課程!