------- android培訓、java培訓、期待與您交流! ----------
06.01 二維數組概述和格式1的講解
二維數組概述:二維數組其實就是一個元素為一維數組的數組
格式1:數據類型[][] 變量名 = new 數據類型[m][n];
m表示這個二維數組有多少個一維數組,n表示每一個一維數組的元素個數
例:int[][] arr = new int[3][2];
定義了一個二維數組arr
這個二維數組有3個一維數組,名稱是arr[0],arr[1],arr[2]
每個一維數組有2個元素,可以通過arr[m][n]來獲取,表示獲取第m+1個一維數組的第n+1個元素
格式1還可以寫成int arr[][]; int[] arr[]; 但是都不建議
注意:
int x,y; 表示同時定義兩個int型的變量x、y
int[] x,y[]; 表示定義了一個一維數組x,一個二維數組y
例:
1 //定義一個二維數組,有3個一維數組,每個 2 //一維數組有2個元素 3 int[][] arr = new int[3][2]; 4 5 //輸出二維數組名稱 6 System.out.PRintln(arr); //[[I@1175422 7 8 //輸出二維數組的第1個元素一維數組的名稱 9 System.out.println(arr[0]); //[I@949f6910 11 //輸出二維數組的元素12 System.out.println(arr[0][0]); //0
06.02 二維數組格式1的內存圖解
06.03 二維數組格式2的講解
格式2:數據類型[][] 變量名 = new 數據類型[m][];
m表示這個二維數組有多少個一維數組,但沒有直接給出一維數組的元素個數,可以動態的給出
例:
1 int[][] arr = new int[3][]; //定義一個有3個一維數組的二維數組2 arr[0] = new int[2]; //第1個一維數組有2個元素3 arr[1] = new int[3]; //第2個一維數組有3個元素4 arr[2] = new int[1]; //第3個一維數組有1個元素
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[][] arr = new int[3][]; 6 7 System.out.println(arr); //[[I@1175422 8 System.out.println(arr[0]); //null 9 10 //動態的為每一個一維數組分配空間11 arr[0] = new int[2];12 arr[1] = new int[3];13 arr[2] = new int[1];14 15 System.out.println(arr[0]); //[I@949f6916 System.out.println(arr[0][0]); //017 18 arr[1][0] = 100;19 arr[2][0] = 200;20 }21 }
06.04 二維數組格式2的內存圖解
06.05 二維數組格式3的講解
格式3:數據類型[][] 變量名 = new 數據類型[][]{{元素...},{元素...},{元素...}};
簡化版格式:數據類型[][] 變量名 = {{元素...},{元素...},{元素...}};
例:int[][] arr = {{1,2,3},{4,5},{6}};
06.06 二維數組格式3的內存圖解
06.07 二維數組練習1遍歷
二維數組遍歷
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[][] arr = {{1,2,3},{4,5,6},{7,8,9}}; 6 //外循環控制的是二維數組的長度,其實就是一維數組的個數 7 for(int i = 0;i < arr.length;i++) 8 { 9 //內循環控制的一維數組的長度10 for(int j = 0;j < arr[i].length;j++)11 {12 System.out.print(arr[i][j]+" ");13 }14 System.out.println();15 }16 }17 }
06.08 二維數組練習2求和
某公司按照季度和月份統計的數據如下:單位(萬元)
第一季度:22,67,43
第二季度:77,33,88
第三季度:25,43,65
第四季度:11,66,99
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 //將題目的數據用二維數據表示 6 int[][] arr = {{22,67,43},{77,33,88},{25,43,65},{11,66,99}}; 7 8 //定義一個求和變量 9 int sum = 0;10 //遍歷二維數組11 for(int i = 0;i < arr.length;i++)12 {13 System.out.print("第"+(i+1)+"季度分別為:");14 for(int j = 0;j < arr[i].length;j++)15 {16 System.out.print(arr[i][j]+"萬 ");17 sum += arr[i][j];18 }19 System.out.println();20 }21 System.out.println("總和為:"+sum+"萬元");22 }23 }
運行結果:
第1季度分別為:22萬 67萬 43萬第2季度分別為:77萬 33萬 88萬第3季度分別為:25萬 43萬 65萬第4季度分別為:11萬 66萬 99萬總和為:639萬元
06.09 二維數組練習3楊輝三角
打印楊輝三角形(行數可以鍵盤錄入)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Scanner sc = new Scanner(System.in); 6 System.out.println("請輸入一個數:"); 7 int n = sc.nextInt(); 8 9 //根據鍵盤錄入的數據創建一個二維數組10 int[][] arr = new int[n][n];11 12 //給二維數組的任何一行的第一列和最后一列賦值為113 for(int i = 0;i < arr.length;i++)14 {15 arr[i][0] = 1; //任何一行第一列16 arr[i][i] = 1; //任何一行最后一列17 }18 19 for(int i = 2;i < arr.length;i++)20 {21 for(int j = 1;j <= i-1; j++)22 {23 //每一個數據是它上一行的前一列和它上一行的本列之和24 arr[i][j] = arr[i-1][j-1] + arr[i-1][j];25 }26 }27 28 //遍歷二維數組29 for(int i = 0;i < arr.length;i++)30 {31 for(int j = 0;j <= i;j++)32 {33 System.out.print(arr[i][j]+"/t");34 }35 System.out.println();36 }37 }38 }
06.10 思考題1Java中的參數傳遞問題
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int a = 10; 6 int b = 20; 7 System.out.println("a:"+a+",b:"+b); //a:10,b:20 8 change(a,b); 9 System.out.println("a:"+a+",b:"+b); //a:10,b:2010 11 int[] arr = {1,2,3,4,5};12 change(arr);13 System.out.println(arr[1]); //414 }15 public static void change(int a,int b) 16 {17 System.out.println("a:"+a+",b:"+b); //a:10,b:2018 a = b;19 b = a + b;20 System.out.println("a:"+a+",b:"+b); //a:20,b:4021 }22 23 public static void change(int[] arr)24 {25 for(int x=0; x<arr.length; x++)26 {27 if(arr[x]%2==0)28 {29 arr[x]*=2;30 }31 }32 }33 }
Java中的參數傳遞問題:
基本類型:形式參數的改變不影響實際參數
引用類型:形式參數的改變直接影響實際參數
06.11 思考題2加密問題的分析
數據加密問題
某個公司采用公用電話傳遞數據信息,數據是小于8位的整數,為了確保安全,在傳遞過程中需要加密,加密規則如下:
首先將數據倒序,然后將每位數字都加上5,再用和除以10的余數代替該數字,最后將第一位和最后一位數字交換。請任意給定一個小于8位的整數,然后,把加密后的結果在控制臺打印出來。
如輸入123456 輸出609871
分析:
1.數據是小于8位的整數,可以定義一個int類型的數據
2.加密規則
a:首先將數據倒序
b:然后將每位數字都加上5,再用和除以10的余數代替該數字
c:最后將第一位和最后一位數字交換
3.把加密后的結果輸出在控制臺
通過分析,需要將數據變成數組
1.定義一個數據
2.定義一個數組,int[] arr = new int[8]; //不可能超過8
在賦值的時候,用一個變量記錄索引的變化。
3.獲取每一個數據
int ge = number%10
int shi = number/10%10
int bai = number/100%10
06.12 思考題2加密問題的代碼的實現
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 //定義一個數據 6 int num = 123456; 7 //定義一個數組 8 int[] arr = new int[8]; 9 //獲取數據中的每一位數據存儲到數組中10 int index = 0;11 while(num > 0)12 {13 arr[index] = num % 10;14 index++;15 num /= 10;16 }17 18 for(int i = 0;i < index ;i++)19 {20 arr[i] += 5;21 arr[i] %= 10;22 }23 24 int temp = arr[0];25 arr[0] = arr[index - 1];26 arr[index - 1] = temp;27 28 for(int i = 0;i < index;i++)29 {30 System.out.print(arr[i]+" ");31 }32 System.out.println();33 }34 }
06.13 思考題2加密問題的代碼改進
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Scanner sc = new Scanner(System.in); 6 System.out.print("原始數據:"); 7 int num = sc.nextInt(); 8 //定義一個數組 9 int[] arr = new int[8];10 //獲取數據中的每一位數據存儲到數組中11 int index = 0;12 while(num > 0)13 {14 arr[index] = num % 10;15 index++;16 num /= 10;17 }18 19 for(int i = 0;i < index ;i++)20 {21 arr[i] += 5;22 arr[i] %= 10;23 }24 25 int temp = arr[0];26 arr[0] = arr[index - 1];27 arr[index - 1] = temp;28 29 System.out.print("加密數據:");30 for(int i = 0;i < index;i++)31 {32 System.out.print(arr[i]);33 }34 System.out.println();35 }36 }
06.14 思想引入和面向過程思想概述
通過前面的講解我們知道類是可以存放方法的,所以,我們就考慮使用類來封裝這多個方法,將來再做數組的操作時,不用去找具體的方法,先找到這個類,然后使用這個類中的方法。這就是面向對象思想的編程方式。
面向過程開發,其實就是面向著具體的每一個步驟和過程,把每一個步驟和過程完成,然后由這些功能方法相互調用,完成需求。
面向過程的代表語言:C語言
06.15 面向對象思想概述
面向對象思想概述:面向對象是基于面向過程的編程思想
面向過程:對于面向過程思想,強調的是過程(動作)。例如C語言就是面向過程語言
面向對象:對于面向對象思想,強調的是對象(實體)。例如C++ 、Java 、C#就是面向對象的語言
面向對象思想特點:
1.面向對象是一種常見的思想,符合人們的思考習慣
2.面向對象的出現,將復雜的問題簡單化
3.面向對象的出現,讓曾經在過程中的執行者,變成了對象中的指揮者
06.16 大象裝進冰箱案例
把大象裝進冰箱
面向過程:動作
1:打開冰箱門
2:裝進大象
3:關閉冰箱門
面向對象:在用面向對象思想體現的時候,給出一個三句話使用規則,讓我們更符合面向對象思想
1.有哪些類
2.每個類有哪些東西
3.類與類直接的關系是什么
把大象裝進冰箱的分析(如何分析有哪些類呢?UML。名詞提取法。)
1.有哪些類:大象、冰箱、Demo
2.每個類有哪些東西:[大象:進去]、[冰箱:開門關門]、[Demo:main方法]
3.類與類直接的關系:Demo中使用大象和冰箱類的功能
06.17 開發,設計以及特征
面向對象開發:就是不斷的創建對象,使用對象,指揮對象做事情。
面向對象設計:就是在管理和維護對象之間的關系。
面向對象特征:
封裝(encapsulation)
繼承(inheritance)
多態(polymorphism)
06.18 類與對象
如何表示一個現實世界事物:
屬性就是該事物的描述信息
行為就是該事物能夠做什么
我們學習的Java語言最基本單位是類,所以,我們就應該把事物用一個類來體現
事物的屬性與行為在類中對應成員變量與成員方法
類是一組相關的屬性和行為的集合
對象是該類事物的具體體現
類是抽象的,概念的,代表一類事物,例如人類、貓類
對象是具體的,實際的,代表一個具體的事物
類是事物的描述,對象是該類事物的實例,在java中通過new創建
06.19 類的定義概述
類與對象的關系圖:
上圖中圖紙就是類,每一個汽車就是對象
現實世界的事物
屬性人的身高,體重等
行為人可以學習,吃飯等
Java中用class描述事物也是如此
成員變量就是事物的屬性
成員方法就是事物的行為
定義類其實就是定義類的成員(成員變量和成員方法)
06.20 學生類的定義
如何定義學生類
按照事物到類的過程一步步分析
學生事物:
屬性:姓名,年齡,地址...
行為:學習,吃飯,睡覺...
把事物要轉換為對應的類:
學生類:
成員變量:姓名,年齡,地址...
成員方法:學習,吃飯,睡覺...
1 class Student 2 { 3 //定義變量 4 //姓名 5 String name; 6 //年齡 7 int age; 8 //地址 9 String address;10 11 //定義方法12 //學習的方法13 public void study() 14 {15 System.out.println("學生愛學習");16 }17 18 //吃飯的方法19 public void eat() 20 {21 System.out.println("學習餓了,要吃飯");22 }23 24 //睡覺的方法25 public void sleep() 26 {27 System.out.println("學習累了,要睡覺");28 }29 }
06.21 手機類的定義
手機事物:
屬性:品牌,價格,顏色...
行為:打電話,發短信,玩游戲...
手機類:
成員變量:品牌,價格,顏色
成員方法:打電話,發短信,玩游戲
1 class Phone 2 { 3 //品牌 4 String brand; 5 //價格 6 int price; 7 //顏色 8 String color; 9 10 //打電話的方法11 public void call(String name) 12 {13 System.out.println("給"+name+"打電話");14 }15 16 //發短信的方法17 public void sendMessage() 18 {19 System.out.println("群發短信");20 }21 22 //玩游戲的方法23 public void playGame()24 {25 System.out.println("玩游戲");26 }27 }
06.22 學生類的使用
創建對象:類名對象名 = new 類名();
使用成員變量:對象名.成員變量
使用成員方法:對象名.成員方法
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Student s = new Student(); 6 System.out.println(s.name+".."+s.age+".."+s.address); 7 //給成員變量賦值 8 s.name = "小明"; 9 s.age = 25;10 s.address = "上海";11 12 System.out.println(s.name+".."+s.age+".."+s.address);13 //調用方法14 s.study();15 }16 }17 18 class Student 19 {20 //定義變量21 //姓名22 String name;23 //年齡24 int age;25 //地址26 String address;27 28 //定義方法29 //學習的方法30 public void study() 31 {32 System.out.println("學生愛學習");33 }34 35 //吃飯的方法36 public void eat() 37 {38 System.out.println("學習餓了,要吃飯");39 }40 41 //睡覺的方法42 public void sleep() 43 {44 System.out.println("學習累了,要睡覺");45 }46 }
運行結果:
null..0..null小明..25..上海學生愛學習
06.23 手機類的使用
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Phone p = new Phone(); 6 System.out.println(p.brand+"..."+p.price+"..."+p.color); 7 //給成員變量賦值 8 p.brand = "諾基亞"; 9 p.price = 450;10 p.color = "黑色";11 System.out.println(p.brand+"..."+p.price+"..."+p.color);12 13 //調用成員方法14 p.call("小明");15 }16 }17 18 class Phone 19 {20 //品牌21 String brand;22 //價格23 int price;24 //顏色25 String color;26 27 //打電話的方法28 public void call(String name) 29 {30 System.out.println("給"+name+"打電話");31 }32 33 //發短信的方法34 public void sendMessage() 35 {36 System.out.println("群發短信");37 }38 39 //玩游戲的方法40 public void playGame()41 {42 System.out.println("玩游戲");43 }44 }
運行結果:
null...0...null諾基亞...450...黑色給小明打電話
06.24 一個對象的內存圖
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Phone p = new Phone(); 6 System.out.println(p.brand+"..."+p.price+"..."+p.color); 7 8 p.brand = "諾基亞"; 9 p.price = 450;10 p.color = "黑色";11 System.out.println(p.brand+"..."+p.price+"..."+p.color);12 13 p.call("小明");14 }15 }16 17 class Phone 18 {19 String brand;20 int price;21 String color;22 23 public void call(String name) 24 {25 System.out.println("給"+name+"打電話");26 }27 }
06.25 二個對象的內存圖
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Phone p1 = new Phone(); 6 p1.brand = "諾基亞"; 7 p1.price = 450; 8 p1.color = "黑色"; 9 System.out.println(p1.brand+"..."+p1.price+"..."+p1.color);10 p1.call("小明");11 12 Phone p2 = new Phone();13 p2.brand = "小米";14 p2.price = 1699;15 p2.color = "白色";16 System.out.println(p2.brand+"..."+p2.price+"..."+p2.color);17 18 p2.call("旺財");19 }20 }21 22 class Phone 23 {24 String brand;25 int price;26 String color;27 28 public void call(String name) 29 {30 System.out.println("給"+name+"打電話");31 }32 }
06.26 三個對象的內存圖
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 Phone p1 = new Phone(); 6 p1.brand = "諾基亞"; 7 p1.price = 450; 8 p1.color = "黑色"; 9 System.out.println(p1.brand+"..."+p1.price+"..."+p1.color);10 11 Phone p2 = new Phone();12 p2.brand = "小米";13 p2.price = 1699;14 p2.color = "白色";15 System.out.println(p2.brand+"..."+p2.price+"..."+p2.color);16 17 Phone p3 = p1;18 System.out.println(p3.brand+"..."+p3.price+"..."+p3.color);19 p3.brand = "魅族";20 p3.price = 2999;21 p3.color = "金色";22 System.out.println(p1.brand+"..."+p1.price+"..."+p1.color);23 }24 }25 26 class Phone 27 {28 String brand;29 int price;30 String color;31 32 public void call(String name) 33 {34 System.out.println("給"+name+"打電話");35 }36 }
新聞熱點
疑難解答