PIXNET Logo登入

Cedric's 學習備忘錄

跳到主文

程式語言、英文學習備忘

部落格全站分類:不設分類

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 1月 09 週六 201022:54
  • 練習Collections類的sort方法排序陣列









import java.util.*;
public class TestSort {
    public static void main(String[] args) {
        int[] a = {1, 3, 6, 7, 0, 2, 5, 8, 4, 9};
        
        List l = new LinkedList();
   
        for(int i=0; i<a.length; i++){
            l.add(a[i]); //將a陣列元素依序放入List中
        }
        Collections.sort(l); //正序排序
        System.out.println(l);
        Collections.reverse(l); //逆序排序
        System.out.println(l);
        System.out.println(Collections.binarySearch(l,5));
        //二分搜尋法,找到5的位置
        
        Collections.shuffle(l); //亂序排序
        System.out.println(l);
    }
    
    
}




(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(4,009)

  • 個人分類:Java備忘錄
▲top
  • 12月 29 週二 200920:37
  • collection

collection.PNG
 
 
 
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(75)

  • 個人分類:Java備忘錄
▲top
  • 12月 28 週一 200922:35
  • java.io.File類練習

利用File建立出 dirA,dirA下再建立dirB,dirB下建立一個檔案file.txt
第一次執行時因為沒有file.txt檔案,所以第一次執行時會先建立該目錄及檔案
第二次執行後便會列印文件的名稱,並且列印檔案內容的長度

(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(1) 人氣(28,193)

  • 個人分類:Java備忘錄
▲top
  • 12月 26 週六 200915:30
  • StringBuffer類方法練習

 
 
 

(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(315)

  • 個人分類:Java備忘錄
▲top
  • 12月 26 週六 200912:29
  • String類方法練習







public class TestString {
    public static void main(String[] args) {
        String s1 = "AaaaABBBBcc&^%adfsfdCCOOkk99876 _haHA";           
        char cr;
        int a = 0, b = 0, c = 0;
        
        for(int i=0; i<s1.length(); i++) {
            cr = s1.charAt(i);
            if(cr >= 'a' && c<= 'z'){
                b++;
            } else if(cr >= 'A' && c<= 'Z') {
                a++;
            } else {
                c++;
            }
        }
        
        
        for(int i=0; i<s1.length(); i++){
            cr = s1.charAt(i);
            if(Character.isLowerCase(cr)) {
                a++;
            } else if(Character.isUpperCase(cr)) {
                b++;
            } else {
                c++;
            }
        }
            System.out.println("字串共有" + s1.length() + "個字");
            System.out.println("大寫字母數有:" + a + "個");
            System.out.println("小寫字母數有:" + b + "個");
            System.out.println("其他符號有:" + c + "個");
            System.out.println();
   
    String s2 = "sunjavahpjavaokjavajjavahahajavajavagoodjava";
    String s3 = "java";
    int index = -1, count = 0;
    
    while((index = s2.indexOf(s3)) != -1) {
        s2 = s2.substring(index + s3.length());
        count ++;
    }
    System.out.println("共找到\"" + count + "\"個java字串");
    
    }//main end
    
    
}//class end



(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(474)

  • 個人分類:Java備忘錄
▲top
  • 12月 25 週五 200902:14
  • Bubble Sort 泡泡排序法

泡泡排序法的原理是將一組數字中的第一位與後一位相比較,若後一位數字較大,則位置對調,再將第二位數與第三位數做比較,若後一數字較大,再對調位置
比如說:
4 3 2 5 1,五個數字比較的話,第一位與第二位比較,也就是4與3做比較,比較結果成為
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(22,912)

  • 個人分類:Java備忘錄
▲top
  • 12月 23 週三 200921:46
  • 學員成績管理系統 1.0

import java.io.*;
import java.util.*;
public class ScoreSystem {
    static Data[] d = new Data[5];
    static BufferedReader input;
    
    public static void main(String[] args) {
        try {
            menu();
        } catch (IOException e)    {
            System.out.println("程式出現輸出入錯誤,請參考以下錯誤代碼");
            e.printStackTrace();
        }
    }//main() end
    
    
    static void menu() throws IOException {
        System.out.println("---------------學員成績管理系統---------------");
        System.out.println("1.顯示所有資料    2.增/修學員資料    3.離開系統");
        System.out.print("請選擇要執行的項目:");
        input = new BufferedReader(new InputStreamReader(System.in));
                
        try {
            int sel = Integer.parseInt(input.readLine());
            while(true){
                switch (sel){
                    case 1:
                        dis_data();
                        break;
                    case 2:
                        mod_data();
                        break;
                    case 3:
                        System.out.println();
                        System.out.print("謝謝您的使用");
                        System.exit(0);
                        break;
                    default :
                        System.out.println();
                        System.out.println("輸入錯誤!請輸入選單中選項");
                        System.out.println();
                        menu();
                        break;
                }
            }//while end
        } catch(NumberFormatException e) {
            System.out.println();
            System.out.println("輸入錯誤!請輸入選單中選項");
            System.out.println();
            menu();
        }//try catch end
    }//menu() end
    
    
    static void dis_data() throws IOException {
        System.out.println();
        System.out.println("===============列印所有學生成績===============");
        //System.out.printf("%2s%5s%6s%6s%6s%6s%6s","學號","姓名","國文","數學","英文","總分","平均");
        System.out.print("學號\t姓名\t國文\t數學\t英文\t總分\t平均");
        System.out.println();        
        try {
            for(int i=0; i<d.length; i++){
                System.out.printf("%3s",d[i].num);
                System.out.printf("%4s",d[i].name);
                System.out.printf("%5s",d[i].mandarin);
                System.out.printf("%5s",d[i].meth);
                System.out.printf("%5s",d[i].english);
                System.out.printf("%5s",d[i].sum);
                System.out.printf("%5s",d[i].avg + "\n");
            }
            System.out.println();
        } catch(NullPointerException e) {
            System.out.println();
            System.out.println("學員資料不夠完整,請輸入完整學員資料");
            System.out.println();
            menu();    
        }//try catch end    
    }//dis_data() end
    
    
    static void mod_data() throws IOException {
        input = new BufferedReader(new InputStreamReader(System.in));
        Scanner sc = new Scanner(System.in);
        try {
            System.out.print("您要增/修的第幾筆的學員資料? ");
            int x = sc.nextInt();
            d[x-1] = new Data();
            System.out.print("請輸入學員學號:");
            String num = input.readLine();
            d[x-1].num = num;
            System.out.print("請輸入學員姓名:");
            String name = input.readLine();
            d[x-1].name = name;
            System.out.print("請輸入國文分數:");
            double mandarin = Double.parseDouble(input.readLine());
            d[x-1].mandarin = mandarin;
            System.out.print("請輸入數學分數:");
            double meth = Double.parseDouble(input.readLine());
            d[x-1].meth = meth;
            System.out.print("請輸入英文分數:");
            double english = Double.parseDouble(input.readLine());
            d[x-1].english = english;
            d[x-1].sum = d[x-1].mandarin + d[x-1].meth + d[x-1].english;
            d[x-1].avg = (d[x-1].mandarin + d[x-1].meth + d[x-1].english) / 3;
            System.out.println("增/修完成");
        } catch(NumberFormatException e) {
            System.out.println();
            System.out.println("輸入錯誤,請輸入阿拉伯數字");
            System.out.println();
            mod_data();
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println();
            System.out.println("輸入錯誤,此系統只能有5個學員資料,ㄎㄎ");
            System.out.println();
            mod_data();
        }
        
        try {
            while(true) {    
                System.out.print("是否要繼續增修? 1.是    2.否(回主選單): ");
                int sel = sc.nextInt();
                switch(sel) {
                    case 1:
                        mod_data();
                        break;
                    case 2:
                        menu();
                        break;
                    default:
                        System.out.println();
                        System.out.println("輸入錯誤!回主選單");
                        System.out.println();
                        menu();
                        break;
                }
            }
        } catch(InputMismatchException e) {
            System.out.println();
            System.out.println("輸入錯誤!回主選單");
            System.out.println();
            menu();
        }
    }//mod_data() end
}//ScoreSystem class end
class Data {
    String num, name;
    double mandarin, meth, english, sum, avg;
    void Data(String num, String name, double mandarin, double meth, double english, double sum, double avg){
    }
}
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(846)

  • 個人分類:Java備忘錄
▲top
  • 12月 14 週一 200923:49
  • 異常(Exception)的概念

異常的概念.jpg
異常指的是程式運行期間所出現的錯誤,運行時出錯可依照編譯器所顯示出的錯誤訊息及行號來了解錯誤發生的原因
 
下圖為異常throwable類的子類分支圖
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(645)

  • 個人分類:Java備忘錄
▲top
  • 12月 13 週日 200900:13
  • 物件導向(Object-oriented)重點

1. 內存解析必須搞清楚,data seg、code seg、stack、heap
2. 物件和類的概念
3. 類(物件)之間的關係
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(0) 人氣(1,196)

  • 個人分類:Java備忘錄
▲top
  • 12月 08 週二 200917:54
  • 抽象類 (abstract)

1. 用abstract 修飾類時,則此類被稱之為抽象類
2. 含有抽象方法的類必須被聲明為抽象類,抽象類必須被繼承、抽象方法必須被重寫,否則子類就也要定義為抽象類及含有抽象方法,若子類繼承後沒有重寫方法,則出現錯誤 (Cat is not abstract and does not override abstract method enjoy() in Animal)
3. 抽象類不能被實例化,也就是不能以new產生新的物件,否則會出現錯誤 (Animal is abstract; cannot be instantiated)
(繼續閱讀...)
文章標籤

ced425 發表在 痞客邦 留言(1) 人氣(3,620)

  • 個人分類:Java備忘錄
▲top
12»

最新文章

  • FreeBSD 9.0 + apache + mono = .net axpx 動態網站伺服器
  • 【FreeBSD 8.2、9.0】Postfix 2.8.5 + dovecot 2.0.15 + MySQL 5.5.16 + PostfixAdmin 2.3.4 基本安裝
  • 何謂OA
  • RAID功能說明
  • 修改Tomcat 7 port
  • Tomcat 常用指令
  • 修改tomcat 7的管理者帳號及密碼
  • Yahoo拍賣搜尋引擎設定
  • MAC 安裝 Tomcat 7.0.8
  • 練習Collections類的sort方法排序陣列

文章分類

  • FreeBSD (2)
  • 技術文件 (2)
  • Web Design (1)
  • MAC (4)
  • 翻譯學英文 (3)
  • Java備忘錄 (18)
  • 未分類文章 (1)

文章精選

文章搜尋