[Java] Ch6 Sorting Array與 Enum用法


sorting array 
  • public class sort {
  •         public static void main(String[] args){
  •                 double[] a={1.0,3.4, 8.4, 5.3, 6.9, 7.3, 2.9, 3.8};
  •                 System.out.println(Arrays.toString(a));
  •                 sort(a);
  •                 System.out.println(Arrays.toString(a));
  •         }
  •         public static void sort(double[] buf){
  •                 for (int i = 0;i<buf.length;i++){
  •                         int smallest = indexOfSmallest(buf, i);
  •                         swap(buf, i, smallest);
  •                 }
  •         }

  •         public static int indexOfSmallest(double[] buf, int start){
  •                 int smallest = start;
  •                 for(int i = start + 1; i < buf.length; i++){
  •                         if(buf[smallest]> buf[i]){
  •                                 smallest = i;
  •                         }
  •                 }
  •                 return smallest;
  •         }
  •         public static void swap(double[] buf,int i,int j){
  •                 double tmp = buf[i];
  •                 buf[i] = buf[j];
  •                 buf[j] = tmp;
  •         }
  • }
Swap 互換兩個整數
方法一:
  • int x = 10, y = 20;
  • System.out.printf("x = %d y = %d\n",x,y);
  • int tmp = x; x = y; y = tmp;
  • System.out.printf("x = %d y = %d\n",x,y);
方法二:
利用Exclusive OR運算來做Swap
  •             // 利用 exclusive or 來做swap ! (此方法只限制在整數才可用)
  •             int x = 1;
  •             int y = 2;
  •             System.out.printf("x = %d y = %d\n",x,y);
  •             x ^= y; y ^= x; x ^= y;
  •             System.out.printf("x = %d y = %d\n",x,y);
  •                 
  •             //原理說明
  •             int x1,y1,x2;
  •             x1 = x^y;
  •             y1 = x1^y; // x^y^y 這樣等於原來的 x
  •             x2 = x1^y1; // 等同於 x1^原來的 x 會等於--> 原本的 y
                
Enum
使用情境
想要做一個限制範圍內的變數
Week2.java
  • public enum Week2 {
  •         SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY
  • }
Enumeration2.java
  • public class Enumeration2 {
  •         public static void main(String[] args){       
  •                 Week2 today, someday;         
  •                 today = Week2.MONDAY;
  •                 someday = null;
  •                 someday = Week2.valueOf("SUNDAY");
  •                 System.out.println(someday);
  •                 
  •                 switch(today){ // switch 的後放可以放 int char byte
  •                 case SATURDAY: case SUNDAY:
  •                         System.out.println("today is weekend");
  •                         break;
  •                 default:
  •                         System.out.println("today is workday");
  •                 }                
  •                 Week2[] allday = Week2.values();
  •                 for( Week2 i:allday){
  •                         System.out.println(i);
  •                 }
  •         }
  • }

使用for each 把所有結果印出來
  • Week2[] allday = Week2.values();
  • for( Week2 i : allday){
  •           System.out.println(i);
  • }






留言

這個網誌中的熱門文章

[筆記] CRLF跟LF之區別 --- 隱形的 bug

[ML筆記] Batch Normalization

[筆記] 統計實習(1) SAS 基礎用法 (匯入資料並另存SAS新檔,SUBSTR,計算總和與平均,BMI)

[ML筆記] Ensemble - Bagging, Boosting & Stacking