1. 程式人生 > >java SE常用的API和一些常用Test

java SE常用的API和一些常用Test

Day01(迴文,驗證碼)

Test01:

1:輸出字串"HelloWorld"的字串長度str.length();

 2:輸出"HelloWorld""o"的位置str.indexOf('o')

 3:輸出"HelloWorld"中從下標5出開始第一次出現"o"的位置str.indexOf('o',5)

 4:擷取"HelloWorld"中的"Hello"並輸出str.substring(0, 5)

 5:擷取"HelloWorld"中的"World"並輸出str.substring(5)

 6:將字串"  Hello   "中兩邊的空白去除後輸出str.trim()

 7:輸出"HelloWorld"中第6個字元

"W"str.charAt(5)

 8:輸出"HelloWorld"是否是以"h"開頭和"ld"結尾的。str.startsWith("h")str.endsWith("ld")

 9:"HelloWorld"分別轉換為全大寫和全小寫並輸出。str.toUpperCase()str.toLowerCase()

Test02:

"大家好!"修改為:"大家好!我是程式設計師!"並輸出。

然後將"大家好!我是程式設計師!"修改為:"大家好!我是優秀的程式設計師!"並輸出

然後再修改為:"大家好!我是牛牛的程式設計師!"並輸出

然後在修改為:"我是牛牛的程式設計師!"並輸出

public static void main(String[] args) {
    StringBuilder builder = new StringBuilder("大家好!");
    builder.append("我是程式設計師!");
    System.out.println(builder.toString());
    builder.insert(6,"優秀的");
    System.out.println(builder.toString());
    builder.replace(6, 8, "牛牛");
    System.out.println(builder.toString());
    builder.delete(0, 4);
    System.out.println(builder.toString());

}

Test03: 檢查一個字串是否為迴文

迴文:正著念與反著念一樣,例如:上海自來水來自海上

public class Test03 {
    public static void main(String[] args) {
        String str = "上海自來水來自海上";
        if(check(str)){
            System.out.println("是迴文");
        }else{
            System.out.println("不是迴文");
        }
    }
    public static boolean check(String str){
        //charAt實現
        for(int i=0;i<str.length()/2;i++){
            if(str.charAt(i)!=str.charAt(str.length()-1-i)){
                return false;
            }
        }
        return true;

//StringBuilder實現
// String str1 = new StringBuilder(str).reverse().toString();
// return str.equals(str1);
    }
}

Test04: 要求使用者從控制檯輸入一個email地址,然後獲取該email的使用者名稱(@之前的內容)

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("請輸入一個email地址");
    String email = scanner.nextLine();
    if(email.indexOf("@")<1){
        System.out.println("缺少@或沒有使用者名稱");
    }else{
        String username = email.substring(0, email.indexOf("@"));
        System.out.println("使用者名稱:"+username);
    }
}

Test05:隨機生成一個5位的英文字母驗證碼(大小寫混搭)

然後將該驗證碼輸出給使用者,然後要求使用者輸入該驗證碼,大小寫不限制。

 然後判定使用者輸入的驗證碼是否有效(無論使用者輸入大小寫,只要字母都正確即可)

public class Test05 {
    public static void main(String[] args) {
        String str = random();
        System.out.println("驗證碼為:"+str);
        System.out.println("請輸入上述驗證碼:");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        if(str.toUpperCase().equals(input.toUpperCase())){
            System.out.println("輸入正確");
        }else{
            System.out.println("輸入錯誤");
        }

    }
    /    **
     * 生成驗證碼
     * @return
     */
    public static String random(){
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<5;i++){
            if(random.nextInt(2)==1){
                builder.append((char)('a'+random.nextInt(27)));
            }else{
                builder.append((char)('A'+random.nextInt(27)));
            }
        }
            return builder.toString();
        }
    }

Test06要求使用者輸入一個計算表示式,可以使用加減乘除。

只處理一次運算的,不要有連續加減乘除的表示式,且不做小數計算。(:1+2+3)

例如:

1+2

然後經過處理計算結果並輸出:1+2=3

public class Test06 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入一個算式:");
		//這裡可以驗證使用者輸入的是否為多次運算,已提醒使用者違例
		
		String line = scanner.nextLine();
		//符號所在的位置
		int index = -1;
		//判斷是否為加法
		if((index=line.indexOf("+"))>0){
			int number1 = parseInt(line.substring(0, index));
			int number2 = parseInt(line.substring(index+1));
			System.out.println(line+"="+(number1+number2));
		}else if((index=line.indexOf("-"))>0){
			int number1 = parseInt(line.substring(0, index));
			int number2 = parseInt(line.substring(index+1));
			System.out.println(line+"="+(number1-number2));
		}else if((index=line.indexOf("*"))>0){
			int number1 = parseInt(line.substring(0, index));
			int number2 = parseInt(line.substring(index+1));
			System.out.println(line+"="+(number1*number2));
		}else if((index=line.indexOf("/"))>0){
			int number1 = parseInt(line.substring(0, index));
			int number2 = parseInt(line.substring(index+1));
			if(number2==0){
				System.out.println("除數不能為0");
				return;
			}
			System.out.println(line+"="+(number1/number2));
		}
	}
	/**
	 * 將指定的字串轉換為數字
	 * @param str
	 * @return
	 */
	public static int parseInt(String str){
		//最後要生成的數字
		int num = 0;
		//臨時變數,用於計算對應位數的數字
		int flag = 0;
		for(int i=0;i<str.length();i++){
			flag = (str.charAt(i)-48);
			/*
			 * 這裡是將對應的數字計算為對應的位,例如百位數字就要用該數字乘以10的2次方
			 * 得到
			 * 可以用Math的相關方法處理(自行檢視API文件)
			 */
			for(int n=0;n<str.length()-1-i;n++){
				flag*=10;
			}
			num+=flag;
		}
		return num;
	}
}

Day02(正則表示式)

Test01要求使用者輸入一個字串,然後若該字串是一個整數,則轉換為整數後輸出乘以10後的結果

 * 若是小數,則轉換為一個小數後輸出乘以5後的結果,若不是數字則輸出"不是數字"

 * 需要使用正則表示式進行判斷。

public class Test01 {
	public static void main(String[] args) {
		//整數的正則表示式
		String intReg = "\\d+";
		//小數的正則表示式
		String douReg = "\\d+\\.\\d+";
		
		System.out.println("請輸入一個數字:");
		Scanner scanner = new Scanner(System.in);
		String line = scanner.nextLine();
		//判斷是否為整數
		if(line.matches(intReg)){
			int num = Integer.parseInt(line);
			System.out.println("整數:"+num+"*10="+(num*10));
		//判斷是否為小數	
		}else if(line.matches(douReg)){
			double num = Double.parseDouble(line);
			System.out.println("小數:"+num+"*5="+(num*5));
		}else{
			System.out.println("不是數字");
		}
	}
}

Test02將字串123,456,789,012根據","拆分,並輸出拆分後的每一項

public class Test02 {
	public static void main(String[] args) {
		String str = "123,456,789,012";
		String[] arr = str.split(",");
		for(int i =0;i<arr.length;i++){
			System.out.println(arr[i]);
		}
	}
}

Test03輸入一個IP地址,然後將4段數字分別輸出

 *

 * IP的正則表示式參考

public class Test03 {
	public static void main(String[] args) {
		System.out.println("請輸入一個IP地址");
		Scanner scanner = new Scanner(System.in);
		String ip = scanner.nextLine();
		//可以驗證IP地址的格式是否合法
		String[] arr = ip.split("\\.");
		for(int i=0;i<arr.length;i++){
			System.out.println(arr[i]);
		}
	}
}

Test04:將字串"123abc456def789ghi"中的英文部分替換為"#char#"

public class Test04 {
	public static void main(String[] args) {
		String str = "123abc456def789ghi";
		str = str.replaceAll("[a-zA-Z]+", "#char#");
		System.out.println(str);
	}
}

Test05:實現檔案重新命名。

 * 要求使用者輸入一個檔名稱,例如:abc.jpg

 * 然後對該名字進行重新命名,輸出的格式為:系統時間毫秒值.jpg

 * 例如:1465266796279.jpg

public class Test05 {
	public static void main(String[] args) {
		System.out.println("請輸入一個檔名");
		Scanner scanner = new Scanner(System.in);
		String fileName = scanner.nextLine();
		//方式一:按照"."拆分
//		String[] arr = fileName.split("\\.");
//		fileName = System.currentTimeMillis()+"."+arr[1];
//		System.out.println(fileName);
		
		//方式二:或者將檔名做替換
		fileName = fileName.replaceAll("\\w+\\.", System.currentTimeMillis()+".");
		System.out.println(fileName);
		
	}
}

Test06:測試正則表示式,並嘗試編寫規則: 電話號碼可能有3-4位區號,

 * 7-8位號碼:0415-5561111

public class Test06 {
	public static void main(String[] args) {
		String regex = "\\d{3,4}-\\d{7,8}";
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入一個電話號碼:");
		String phoneNumber = scanner.nextLine();
		if(phoneNumber.matches(regex)){
			System.out.println("是電話號碼");
		}else{
			System.out.println("不是電話號碼");
		}
		
	}
}

Test07:和上個test06差不多。

Test08要求使用者輸入若干員工資訊,格式為:

 * name,age,gender,salary;name,age,gender,salary;....

 * 例如:

 * 張三,25,,5000;李四,26,,6000;...

 * 然後將每個員工資訊解析成Person物件。並存入到一個數組中。

 * 然後迴圈陣列,輸出每一個員工資訊(輸出使用toString返回的字串)

public class Test08 {
	public static void main(String[] args) {
		System.out.println("請輸入員工資訊:");
		Scanner scanner = new Scanner(System.in);
		String info = scanner.nextLine();
		//首先根據";"拆分出每個員工資訊
		String[] infoArr = info.split(";");
		//根據拆分出的員工資訊個數建立對應長度的陣列
		Person[] personArr = new Person[infoArr.length];
		/*
		 * 遍歷員工資訊的陣列,將每個員工資訊解析為一個Person物件
		 * 並存入到personArr陣列中
		 */
		for(int i=0;i<personArr.length;i++){
			String personInfo = infoArr[i];
			//按照","拆分一個員工的各項資訊
			String data[] = personInfo.split(",");
			String name = data[0];
			int age = Integer.parseInt(data[1]);
			String gender = data[2];
			int salary = Integer.parseInt(data[3]);
			Person p = new Person(name, age, gender, salary);
			personArr[i]=p;
		}
		System.out.println("解析完畢");
		for(int i=0;i<personArr.length;i++){
			System.out.println(personArr[i]);
		}
		
	}
}

Day03(時間)

Test01使用Date輸出當前系統時間,以及3天后這一刻的時間

public class Test01 {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println("當前日期:"+date);
		
		date.setTime(date.getTime()+1000*60*60*24*3);
		System.out.println("3天后:"+date);
	}
}

Test02將當前系統時間以"yyyy-MM-dd HH:mm:ss"格式輸出

public class Test02 {
	public static void main(String[] args) {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(now));
	}
}

Test03輸入某人生日,格式為"yyyy-MM-dd",輸出到現在為止經過了多少周

public class Test03 {
	public static void main(String[] args) throws ParseException {
		System.out.println("請輸入生日:");
		Scanner scanner = new Scanner(System.in);
		String birthStr = scanner.nextLine();
		//將輸入的字串轉換為Date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date birth = sdf.parse(birthStr);
		//當前系統時間
		Date now = new Date();
		//計算相差的時間
		long time = now.getTime()-birth.getTime();
		//換算為周
		time/=1000*60*60*24*7;
		System.out.println("經過了"+time+"周");
	}
}

Test04輸入一個生產日期格式"yyyy-MM-dd",再輸入一個數字(保質期的天數)

然後經過計算輸出促銷日期,促銷日期為:該商品過期日前2周的週三

public class Test04 {
	public static void main(String[] args) throws ParseException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入一個生產日期:");
		String dateStr = scanner.nextLine();

		System.out.println("請輸入一個保質期天數:");
		int days = Integer.parseInt(scanner.nextLine());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//將生產日期轉換為Date
		Date date = sdf.parse(dateStr);
		
		//建立Calendar計算時間
		Calendar calendar = Calendar.getInstance();
		//表示生產日期
		calendar.setTime(date);
		//計算過期日
		calendar.add(Calendar.DAY_OF_YEAR, days);
		//計算過日期兩週前
		calendar.add(Calendar.DAY_OF_YEAR, -14);
		//設定為當週週三
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		//轉換為Date
		date = calendar.getTime();
		//輸出促銷日期
		System.out.println("促銷日期:"+sdf.format(date));
		
	}
}

Test05實現時間的計算: 要求使用者輸入身份證號,若格式有誤,要求其重新輸入。然後根據身份證號碼輸出20歲生日

 * 所在周的週三的日期。

 * 例如:出生日期:1992-07-1520歲生日:2012-07-15

 * 當週的週三為:2012-07-18

public class Test05 {
	public static void main(String[] args) throws ParseException {
		String regex = "\\d{15}(\\d{2}[0-9xX])?";
		Scanner scanner = new Scanner(System.in);
		//身份證號的字串
		String id = "";
		while(true){
			System.out.println("請輸入身份證號:");
			id = scanner.nextLine();
			if(!id.matches(regex)){
				System.out.println("請輸入正確的身份證號.");
			}else{
				break; 
			}
		}
		//擷取生日的部分
		String birthStr = id.substring(6, 14);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date birth = sdf.parse(birthStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(birth);
		calendar.add(Calendar.YEAR, 20);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		//將計算後的日期按格式輸出
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("日期為:"+sdf1.format(calendar.getTime()));
	}
}

Test06建立一個集合,存放字串"one","two""three"

 * 然後輸出該集合的元素個數。

 * 然後輸出該集合是否包含字串"four"

 * 然後輸出集合是否不含有任何元素

 * 然後清空集合

 * 然後輸出該集合的元素個數

 * 然後輸出集合是否不含有任何元素

public class Test06 {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add("one");
		c.add("two");
		c.add("three");
		System.out.println("元素數量:"+c.size());
		System.out.println("是否包含four:"+c.contains("four"));
		System.out.println("是否是空集:"+c.isEmpty());
		c.clear();
		System.out.println("集合已清空");
		System.out.println("元素數量:"+c.size());
		System.out.println("是否是空集:"+c.isEmpty());
	}
}

Test07要求使用者首先輸入員工數量,然後輸入相應員工資訊,格式為:

 * name,age,gender,salary,hiredate

 * 例如:

 * 張三,25,,5000,2006-02-15

 * 每一行為一個員工資訊,然後將每個員工資訊解析成Emp物件。並存入到一個集合中。

 * 在解析成Emp物件後要先檢視當前集合是否包含該員工,若包含則提示該用於已存在,

 * 否則才存入集合。

 * 然後輸出集合檢視每個員工資訊.

public class Test07 {
	public static void main(String[] args) throws ParseException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入員工數量:");
		int num = Integer.parseInt(scanner.nextLine());
		//建立一個集合用於儲存所有解析出來的員工資訊
		Collection emps = new ArrayList();
		//開始解析工作
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i=1;i<=num;){
			System.out.println("請輸入第"+i+"個員工資訊");
			String info = scanner.nextLine();
			String[] data = info.split(",");
			String name = data[0];
			int age = Integer.parseInt(data[1]);
			String gender = data[2];
			int salary = Integer.parseInt(data[3]);
			Date hiredate = sdf.parse(data[4]);
			Emp emp = new Emp(name,age,gender,salary,hiredate);
			if(emps.contains(emp)){
				System.out.println("該員工已經存在!");
				continue;
			}
			emps.add(emp);
			i++;
		}
		System.out.println(emps);
	}
}

Day04(集合,陣列)

Test01建立一個集合c1,存放元素"one","two","three"

 * 再建立一個集合c2,存放元素"four","five","six"

 * 然後將c2元素全部存入c1集合

 * 然後在建立集合c3,存放元素"one,five"

 * 然後輸出集合c1是否包含集合c3的所有元素

 * 然後將c1集合中的"two"刪除後再輸出c1集合

public class Test01 {
	public static void main(String[] args) {
		Collection<String> c1 = new ArrayList<String>();
		c1.add("one");c1.add("two");c1.add("three");
		System.out.println("c1:"+c1);
		Collection<String> c2 = new ArrayList<String>();
		c2.add("four");c2.add("five");c2.add("six");
		System.out.println("c2:"+c2);
		//c2元素存入c1
		c1.addAll(c2);
		System.out.println("已將c2元素全部存入c1");
		System.out.println("c1:"+c1);
		
		Collection<String> c3 = new ArrayList<String>();
		c3.add("one");c3.add("five");
		System.out.println("c1是否包含c3所有元素:"+c1.containsAll(c3));
		
		//刪除c1中的two
		c1.remove("two");
		System.out.println("刪除了c1集合中的元素two");
		System.out.println(c1);
	}
}

Test02建立一個集合,存放元素"1","$","2","$","3","$","4"

 *   使用迭代器遍歷集合,並在過程中刪除所有的"$"

 *   最後再將刪除元素後的集合使用新迴圈遍歷,並輸出每一個元素。

public class Test02 {
	public static void main(String[] args) {
		Collection<String> c = new ArrayList<String>();
		c.add("1");c.add("$");c.add("2");c.add("$");c.add("3");c.add("$");c.add("4");
		Iterator<String> it = c.iterator();
		while(it.hasNext()){
			String str = it.next();
			if("$".equals(str)){
				it.remove();
			}
		}
		for(String str : c){
			System.out.println(str);
		}
	}
}

Test03建立一個List集合(ArrayList,LinkedList均可)

 * 存放元素"one","two","three","four"

 * 獲取集合第二個元素並輸出。

 * 將集合第三個元素設定為"3"

 * 在集合第二個位置上插入元素"2"

 * 刪除集合第三個元素。

public class Test03 {
	public static void main(String[] args) {
		List<String> list = new LinkedList<String>();
		list.add("one");list.add("two");list.add("three");list.add("four");
		System.out.println("list:"+list);
		String e = list.get(1);
		System.out.println("第二個元素:"+e);
		//設定集合第三個元素
		list.set(2, "3");
		System.out.println("list:"+list);
		//將元素"2"設定到第二個位置上
		list.add(1,"2");
		System.out.println("list:"+list);
		
		
	}
}

Test04建立一個List集合並新增元素0-9

 * 然後獲取子集[3,4,5,6]

 * 然後將子集元素擴大10

 * 然後輸出原集合。

 * 之後刪除集合中的[7,8,9]

public class Test04 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0;i<10;i++){
			list.add(i);
		}
		System.out.println("list:"+list);
		//獲取子集
		List<Integer> subList = list.subList(3, 7);
		for(int i=0;i<subList.size();i++){
			subList.set(i, subList.get(i)*10);
		}
		System.out.println("list:"+list);
		//刪除7-9
		list.subList(7, 10).clear();
		System.out.println("list:"+list);
	}
}

Test05建立一個List集合,並新增元素0-9

 * 將集合轉換為一個Integer陣列並輸出陣列每一個元素

public class Test05 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0;i<10;i++){
			list.add(i);
		}
		Integer[] array = list.toArray(new Integer[list.size()]);
		for(int num : array){
			System.out.println(num);
		}
	}
}

Test06建立一個字串陣列:{"one","two","three"}

 * 然後將該陣列轉換為一個List集合

public class Test06 {
	public static void main(String[] args) {
		String[] arr = {"one","two","three"};
		List<String> list = Arrays.asList(arr);
		System.out.println(list);
 	}
}

Test07建立一個List集合,並存放10個隨機數,然後排序該集合後輸出

public class Test07 {
	public static void main(String[] args) {
		Random random = new Random();
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0;i<10;i++){
			list.add(random.nextInt(100));
		}
		System.out.println("list:"+list);
		Collections.sort(list);
		System.out.println("list:"+list);
	}
}

Test08通過控制檯輸入3個日期(yyyy-MM-dd格式),然後轉換為Date物件後存入集合,然後對該集合排序後輸出所有日期。

public class Test08 {
	public static void main(String[] args) throws ParseException {
		Scanner scanner = new Scanner(System.in);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Date> list = new ArrayList<Date>();
		for(int i=1;i<=3;i++){
			System.out.println("請輸入第"+i+"個日期:");
			String line = scanner.nextLine();
			Date date = sdf.parse(line);
			list.add(date);
		}
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
	}
}

Test09要求使用者輸入若干員工資訊,格式為:

 * name,age,gender,salary,hiredate;name,age,gender,salary,hiredate;....

 * 例如:

 * 張三,25,,5000,2006-02-15;李四,26,,6000,2007-12-24;...

 * 然後將每個員工資訊解析成Emp物件。並存入到一個集合中。

 * 然後迴圈集合,輸出每一個員工資訊(輸出使用toString返回的字串)

 * 然後輸出每個員工的轉正儀式日期。

 * 轉正儀式日期為:入職3個月的當週週五

public class Test09 {
	public static void main(String[] args) throws ParseException {
		List<Emp> list = new ArrayList<Emp>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入員工資訊:");
		String info = scanner.nextLine();
		String[] empArr = info.split(";");
		for(String data:empArr){
			String[] empInfo = data.split(",");
			String name = empInfo[0];
			int age = Integer.parseInt(empInfo[1]);
			String gender = empInfo[2];
			int salary = Integer.parseInt(empInfo[3]);
			Date hiredate = sdf.parse(empInfo[4]);
			Emp e = new Emp(name, age, gender, salary, hiredate);
			list.add(e);
		}
		//輸出員工資訊
		for(Emp e : list){
			System.out.println(e);
		}
		//輸出轉正儀式日期
		for(Emp e : list){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(e.getHiredate());
			//入職3個月後的日期
			calendar.add(Calendar.MONTH, 3);
			//設定為當週的週五
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			System.out.println(e.getName()+"的轉正儀式日期:"+sdf.format(calendar.getTime()));
		}
	}
}

Day05(佇列,棧,Map)

Test01建立一個佇列,存入Integer型別元素1,2,3,4,5

 然後遍歷佇列並輸出每個元素

public class Test01 {
	public static void main(String[] args) {
		Queue<Integer> queue = new LinkedList<Integer>();
		queue.offer(1);queue.offer(2);queue.offer(3);queue.offer(4);queue.offer(5);
		
		while(queue.size()>0){
			System.out.println(queue.poll());
		}
	}
}

Test02建立一個棧,存入Integer型別元素1,2,3,4,5然後遍歷佇列並輸出每個元素

public class Test02 {
	public static void main(String[] args) {
		Deque<Integer> stack = new LinkedList<Integer>();
		stack.push(1);stack.push(2);stack.push(3);stack.push(4);stack.push(5);
		
		while(stack.size()>0){
			System.out.println(stack.pop());
		}
	}
}

Test03要求使用者輸入若干員工資訊,格式為:

 * name,age,gender,salary,hiredate;name,age,gender,salary,hiredate;....

 * 例如:

 * 張三,25,,5000,2006-02-15;李四,26,,6000,2007-12-24;...

 * 然後將每個員工資訊解析成Emp物件。並存入到一個List集合中。

 * 並對集合排序,然後輸出每個員工資訊。

 *

 * 再根據員工的入職時間排序,入職晚的在前,早的在後並

 * 輸出每個員工的資訊。

public class Test03 {
	public static void main(String[] args) throws ParseException {
		//獲取所有員工資訊
		List<Emp> list = getEmp();
		//排序集合
		Collections.sort(list);
		for(Emp e : list){
			System.out.println(e);
		}

		//按照入職時間排序
		System.out.println("按照入職時間從晚到早排序:");
		Comparator<Emp> com = new Comparator<Emp>(){
			public int compare(Emp e1, Emp e2) {
				System.out.println(e1.getHiredate().getTime()-e2.getHiredate().getTime());
				long time = e2.getHiredate().getTime()-e1.getHiredate().getTime();
				return time>0?1:-1;
			}			
		};
		Collections.sort(list,com);
		for(Emp e : list){
			System.out.println(e);
		}
	}
	/**
	 * 該方法的作用是獲取所有使用者輸入的員工資訊並解析為若干Emp例項存入
	 * 一個集合,然後將該集合返回
	 * @return
	 * @throws ParseException
	 */
	public static List<Emp> getEmp() throws ParseException{
		List<Emp> list = new ArrayList<Emp>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入員工資訊:");
		String info = scanner.nextLine();
		String[] empArr = info.split(";");
		for(String data:empArr){
			String[] empInfo = data.split(",");
			String name = empInfo[0];
			int age = Integer.parseInt(empInfo[1]);
			String gender = empInfo[2];
			int salary = Integer.parseInt(empInfo[3]);
			Date hiredate = sdf.parse(empInfo[4]);
			Emp e = new Emp(name, age, gender, salary, hiredate);
			list.add(e);
		}
		return list;
	}
}

Test04建立一個Map,儲存某個學生的成績:

 *在控制檯輸入該學生成績,格式:科目:成績;科目:成績;...

 *例如:  語文:99;數學:98;英語:97;物理:96;化學:95

 *然後輸出物理的成績。

 *然後將化學的成績設定為96

 *然後刪除英語這一項。

 *然後遍歷該Map分別按照遍歷keyEntryvalue的形式輸出該Map資訊。

public class Test04 {
	public static void main(String[] args) {
		Map<String,Integer> map
			= new HashMap<String,Integer>();
		map.put("語文", 99);map.put("數學", 98);map.put("