1. 程式人生 > 實用技巧 >centos6.5中mysql5.7的安裝

centos6.5中mysql5.7的安裝

一、方法的多級呼叫

  • 案例:
    public class Demo01 {
    	public static void main(String[] args) {
    		System.out.println("準備呼叫show01");
    		show01();
    		System.out.println("main方法結束啦");
    	}
    
    	public static void show01() {
    		System.out.println("show01被呼叫啦");
    		
    		System.out.println("在show01中呼叫show02");
    		
    		show02();
    		
    		System.out.println("show01呼叫show02結束");
    		System.out.println("show01呼叫結束啦");
    	}
    
    	public static void show02() {
    		System.out.println("show02被呼叫啦");
    		
    		System.out.println("在show02中呼叫show03");
    		
    		show03();
    		
    		System.out.println("show02呼叫show03結束");
    		System.out.println("show02呼叫結束啦");
    	}
    
    	public static void show03() {
    		System.out.println("show03被呼叫啦");
    		
    		System.out.println("show03啥也沒幹....");
    		
    		System.out.println("show03呼叫結束啦");
    	}
    } 

二、遞迴

  • 在方法中呼叫方法本身
  • 可能會造成無窮遞迴,需要設定合適的出口
  • 案例:
    public class Demo03 {
    	public static void main(String[] args) {
    		int mul = getMul(5);
    		System.out.println(mul);
    	}
    
    	/**
    	 * 獲取階乘的方法
    	 * @param num
    	 * @return
    	 */
    	public static int getMul(int num) {
    		/**
    		 * 5!
    		 * 	5*4*3*2*1 ===  
    		 * 	5*4!
    		 * 		5*4*3!
    		 * 			5*4*3*2!
    		 * 				5*4*3*2*1!
    		 * 
    		 */
    		System.out.println(num);
    		if (num == 1) {
    			return 1;
    		}
    		return num * getMul(num-1);
    	}
    }

三、方法的過載

  • 定義:
    • 在同一個類中,出現了名字相同的方法
    • 他們的引數列表不同
    • 和返回值型別無關
  • 案例:
    public class Demo04 {
    	public static void main(String[] args) {
    		int sum01 = getSum(3,5);
    		
    		int sum02 = getSum(2,3,5);
    		
    		// getSum(int,int)
    		/**
    		 * 在同一個類中
    		 * 出現了名字相同的方法
    		 * 他們的引數列表不同
    		 * 和返回值型別無關
    		 */
    	}
    
    	public static int getSum(int i, int j) {
    		return i + j;
    	}
    	
    	public static int getSum(int a, int b,int c) {
    		return a + b + c;
    	}
    	
    	/*
    	 * public static void getSum(int a, int b,int c) { System.out.println(a + b +
    	 * c);; }
    	 */
    	
    }
  • 引數列表不同的情況
    • 引數列表中的形引數量不同
    • 引數列表中的形引數據型別不同
    • 引數列表中的引數的資料型別順序不同
  • 案例:
    public class Demo05 {
    	public static void main(String[] args) {
    		
    	}
    	
    	/**
    	 * getSum(int,int)
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	public static int getSum(int a,int b) {
    		return a+b;
    	}
    	
    	/**
    	 * getSum(int,int,int)
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	public static int getSum(int a,int b,int c) {
    		return a+b+c;
    	}
    	
    	/**
    	 * getSum(int,double)
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	public static double getSum(int a,double b) {
    		return a+b;
    	}
    	
    	/**
    	 * getSum(double,int)
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	public static double getSum(double a,int b) {
    		return a+b;
    	}
    }  

四、陣列

  • 定義:
    • 一組連續的儲存空間,儲存多個相同資料型別的值
    • 建立陣列時候必須確定其資料型別和長度
    • 陣列中的物件在記憶體中是連續存在的
    • 陣列中的元素都有一個下標索引,下標從0開始到 陣列名.Length-1
    • 陣列第一個元素的地址及時當前陣列的地址
  • 陣列建立語法【四種】
    • 先宣告、在分配空間:資料型別 [ ] 陣列名; ==》陣列名 = new 資料型別[長度];
      • 例如:int [ ] array; ==>array = new int[10];
    • 宣告並分配空間:資料型別 [ ] 陣列名 = new 資料型別[長度];
      • 例如:int [ ] array = new int [10];
    • 宣告並賦值(繁):資料型別 [ ] 陣列名 = new 資料型別[ ] {value1,value2,calue3........}
      • int [ ] array = new int[]{10,20,30,40};
    • 宣告並賦值(簡):資料型別 [ ] 陣列名 = {vallue1,value2,value3....}
      • int [ ] array = {10,20,30,50,60....}
  • 各種型別陣列元素的預設值:
    • 整型【byte、short、int、long】: 0
    • 浮點型【float、double】:0.0
    • 布林型【boolean】:false
    • 字元型【char】:\u0000
    • 引用型別【類、介面、陣列】:null
  • 遍歷陣列:
    public class Test01 {
        public static void main(String[] args) {
            String[] names = {"宋江","吳用","盧俊義","公孫勝","秦明"};
            //遍歷陣列
            for (int i = 0;i<names.length;i++){
                System.out.println(names[i]);
            }
        }
    }
  • 案例【獲取陣列中的最大值】
    • public class Demo07 {
      	public static void main(String[] args) {
      		// 獲取數字中最大的元素
      		int[] arr = new int[] {14234,32,34,344,326235,8454321,9,5565432,42};
      		// 假設第一個元素是最大的
      		int max = arr[0];
      		
      		for (int i = 1; i < arr.length; i++) {
      			// 使用max和後面的每一個元素比較
      			if (arr[i] > max) {
      				// 如果發現比max更大的元素,更改max的值為更大的哪個元素
      				max = arr[i];
      			}
      		}
      		System.out.println("陣列中最大的元素是:" + max);
      	}
      }
  • 獲取最大下標
    • public class Demo08 {
      	public static void main(String[] args) {
      		// 找到最大值第一次出現的下標
      		int[] arr = new int[] {14234,32,34,8454321,8454321,8454321,344,326235,9,5565432,8454321,42};
      		
      		// 假設最大值的下標是0
      		int maxIndex = 0;
      		
      		for (int i = 0; i < arr.length; i++) {
      			// maxIndex位置的元素和後面的所有元素比較,如果發現更大的,修改maxIndex的值
      			if (arr[maxIndex] < arr[i]) {
      				maxIndex = i;
      			}
      		}
      		System.out.println("最大值的下標在" + maxIndex);
      	}
      }
      

五、字串相關練習

  • 案例【使用startsWith、charAt、equals】
  • public class Demo10 {
    	public static void main(String[] args) {
    		// 輸出班級中張姓學員的名字
    		String[] names = {"張三","李四","王五","張柳","田七","張八","週一"};
    		// 遍歷names,獲取每一個人的名字
    		for (int i = 0; i < names.length; i++) {
    			String name = names[i];
    			// 獲取姓氏
    			char c = name.charAt(0);
    			
    			// 比較姓氏是不是張
    			if (c == '張') {
    				System.out.println(name);
    			}
    		}
    		
    		System.out.println("===============");
    		
    		for (int i = 0; i < names.length; i++) {
    			// 獲取每一個人的名字
    			String name = names[i];
    			// 使用字串的startWith方法判定是不是以張開頭
    			if (name.startsWith("張")) {
    				System.out.println(name);
    			}
    		}
    		
    		System.out.println("==============");
    		
    		// 查詢names中有沒有張八這個人
    		for (int i = 0; i < names.length; i++) {
    			// 判斷有沒有張八這個元素
    			String name = names[i];
    			if (name.equals("張八")) {
    				System.out.println(name);
    			}
    		}
    	}
    }