一些練習題,未寫完

package com.gotoheima;

/**
 * 看JDK多少位
 */
public class Test0
{
	public static void main(String[] args)
	{
		String banben = System.getProperty("sun.arch.data.model");
		System.out.println(banben);
	}
}


package com.gotoheima;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 編寫一個類,加強java.io.BufferedReader的ReadLine()方法,使之在讀取某個文件時,能打印出行號。
 *	
 *	就是一個模擬BufferedReadLine的程序
 *	定義一個計數器,定義Reader
 *	模擬ReadLine方法,當讀取一行,計數器+1,遇到\r\n就表示應該換行
 *	關閉緩衝區
 *
 */
public class Test1 
{
	public static void main(String[] args)
	{
		
		MyLineNumReader mnr = null;
		try
		{
			mnr = new MyLineNumReader(new FileReader("Test.txt"));
			
			String line = null;
			
			while ((line = mnr.myReadLine()) != null)
			{
				System.out.println(mnr.getCount()+":\t"+line);
			}
		}
		catch (IOException e)
		{
			System.out.println("讀寫錯誤");
		}
		finally
		{
			try
			{
				if (mnr != null)
					mnr.myClose();
			}
			catch (IOException e)
			{
				System.out.println("流關閉錯誤");
			}
		}
	}
}

//聲明一個類,模擬BufferedReader方法
class MyLineNumReader
{
	private Reader r;
	private int count;
	
	//構造函數,使能夠被建立對象
	MyLineNumReader(Reader r)
	{
		this.r = r;
	}
	
	//定義一個myReadLine方法,功能是讀取一行數據
	public String myReadLine() throws IOException
	{		
			StringBuffer sb = new StringBuffer ();
			
			int num = 0;
			count ++;
			while ((num = r.read()) != -1)
			{
				if (num == '\r')
					continue;
				if (num == '\n')
					return sb.toString();
				else
					sb.append((char)num);
			}
			if (sb.length() != 0)
				return sb.toString();
			return null;
	}
	
	//必要的讀取屬性的方法
	public int getCount() {
		return count;
	}
	
	////必要的修改屬性的方法
	public void setCount(int count) {
		this.count = count;
	}	
	
	//關流方法
	public void myClose() throws IOException
	{
		r.close();
	}
}

package com.gotoheima;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * 編寫程序,將指定目錄下全部的.java文件拷貝到另外一個目的中,將擴展名改成.txt。
 * 	定義一個函數,函數功能是遍歷當前目錄全部非文件夾的文件
 * 	把後綴是.java文件的文件拷貝到另外一個文件夾中
 * 	修改她們的後綴名爲.txt,用String的replace方法。
 *
 */
public class Test3 
{
	public static void main(String[] args) 
	{
		File f = new File("F:\\java\\day20");
		copyFile(f);
	}
	
	//函數功能:把一個文件夾下面的.java結尾的文件複製到copy文件夾下面並更名
	public static void copyFile(File f)
	{
		//建立一個copy文件加,複製後的文件會被裝進來
		File copy = new File("copy");
		copy.mkdir();

		//過濾。java文件
		File[] files = f.listFiles(new FilenameFilter()
		{
			public boolean accept(File f,String name)
			{
				return name.endsWith(".java");
			}
		});
		
		//複製文件並更名
		for (File fileName : files)
		{
			BufferedInputStream bis = null;
			BufferedOutputStream bos = null;
				
			try
			{
				bis = new BufferedInputStream(new FileInputStream(fileName));
				//把後綴名.java改爲.txt
				String newName = fileName.getName().replace(".java",".txt");
				
				bos = new BufferedOutputStream(new FileOutputStream(new File(copy,newName)));
				
				byte[] by = new byte[1024];
				int num = 0;
				
				while ((num = bis.read()) != -1)
				{
					bos.write(by,0,num);
				}
			}
			catch(IOException e)
			{
				throw new RuntimeException("讀寫出錯");
			}
			finally
			{
				try 
				{
					if (bis != null)
						bis.close();
				}
				catch(IOException e)
				{
					throw new RuntimeException("讀取關閉出錯");
				}
				try 
				{
					if (bos != null)
						bos.close();
				}
				catch(IOException e)
				{
					throw new RuntimeException("寫入關閉出錯");
				}
			}
		}
	}
}

package com.gotoheima;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 
 *  ArrayList list = new ArrayList(); 在這個泛型爲Integer的ArrayList中存放一個String類型的對象。
 *  
 *  這裏考察的是反射,獲取ArrayList的字節碼對象
 *  用Method的方法獲取add的字段
 *  繞過編譯階段去添加String類型對象
 *
 */
public class Test4 
{
	public static void main(String[] args) throws Exception
	{
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		Method methodAdd = list.getClass().getMethod("add",Object.class);
		
		methodAdd.invoke(list,"String");
		
		System.out.println(list);
	}
}

package com.gotoheima;

import java.util.Collections;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * 編寫程序,循環接收用戶從鍵盤輸入多個字符串,直到輸入「end」時循環結束,並將全部已輸入的字符串按字典順序倒序打印。
 *	用Scanner掃描多個字符串
 *	判斷,若是掃描的字符串是end則結束循環
 *	若是不是存入集合中,由於考慮排序,因此用TreeSet集合
 *	定義其比較規則,實現comparable
 *	打印
 */
public class Test5 
{
	public static void main(String[] args)
	{
		TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("輸入字符串(end結束):");
		
		while (true)
		{
			String str = sc.next();
			
			if (str.equals("end"))
				break;
			ts.add(str);
		}
		
		System.out.println(ts);
	}
}

package com.gotoheima;

import java.lang.reflect.Method;

/**
 * 
 * 編寫一個類,增長一個實例方法用於打印一條字符串。並使用反射手段建立該類的對象,並調用該對象中的方法
 *
 */
public class Test6 
{
	public static void main(String[] args) throws Exception
	{
		Class<stringDemo> clazz = stringDemo.class;
		
		Method method = clazz.getMethod("printDemo",String.class);
		
		method.invoke(clazz.newInstance(),"Hello java");
	}
}

class stringDemo
{
	public void printDemo(String str)
	{
		System.out.println(str);
	}
}

package com.gotoheima;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * 
 * 定義一個文件輸入流,調用read(byte[] b)方法將exercise.txt文件中的全部內容打印出來(byte數組的大小限制爲5,不考慮中文編碼問題)
 *
 */
public class Test7 
{
	public static void main(String[] args)
	{
		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream("exercise.txt");
			
			byte[] b = new byte[5];
			int num = 0;
			while ((num = fis.read(b)) != -1)
			{
				System.out.print(new String(b,0,num));				
			}
		}
		catch(IOException e)
		{
			throw new RuntimeException("讀寫出錯!");
		}
		finally
		{
			try 
			{
				if (fis != null)
					fis.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("關閉出錯!");
			}
		}
			
		
		
	
	}
}

package com.gotoheima;

import java.util.Scanner;

/**
 * 編寫一個程序,它先將鍵盤上輸入的一個字符串轉換成十進制整數,而後打印出這個十進制整數對應的二進制形式。
 * 這個程序要考慮輸入的字符串不能轉換成一個十進制整數的狀況,並對轉換失敗的緣由要區分出是數字太大,仍是其中包含有非數字字符的狀況。
 * 提示:十進制數轉二進制數的方式是用這個數除以2,餘數就是二進制數的最低位,接着再用獲得的商做爲被除數去除以2 ,此次獲得的餘數就是次低位,如此循環,直到被除數爲0爲止。
 * 其實,只要明白了打印出一個十進制數的每一位的方式(不斷除以10,獲得的餘數就分別是個位,十位,百位),就很容易理解十進制數轉二進制數的這種方式。
 *
 *
 */
public class Test8
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入一個字符串:");
		String str = sc.next();
		int in = 0;
		
		try
		{
			in = Integer.parseInt(str);
			long shang = in/2;
			long yu = in%2;
			
			StringBuffer sb = new StringBuffer();
			while (shang != 0)
			{
				yu = shang%2;
				shang = shang/2;
				sb.append(yu);
			}
			System.out.println(sb.reverse().toString());			
		}
		
		catch (Exception e)
		{
			String regx = ".*\\D+.*";
			
			if (str.matches(regx))
				throw new RuntimeException("出現非數字");
			else
				throw new RuntimeException("範圍超出");
		}	
	}
}

package com.gotoheima;
/**
 * 將字符串中進行反轉。abcde -->edcba
 *	定義一個StringBuffer
 *	reverse方法
 *
 */
public class Test9 
{
	public static void main(String[] args)
	{
		StringBuffer sb = new StringBuffer("abcde");
		
		sb.reverse();
		
		System.out.print(sb.toString());
	}
}

package com.gotoheima;

import java.util.HashSet;
import java.util.Random;

/**
 * 編寫一個程序,獲取10個1至20的隨機數,要求隨機數不能重複。
 * 
 *
 */
public class Test10 
{
	public static void main(String[] args)
	{
		Random r = new Random();
		HashSet<Integer> hs = new HashSet<Integer>();
		
		while (hs.size()<10)
		{
			hs.add(r.nextInt(20)+1);
		}
		System.out.println(hs);
	}
}

package com.gotoheima;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 取出一個字符串中字母出現的次數。如:字符串:"abcde%^kka27qoq" ,輸出格式爲: a(2)b(1)k(2)...
 *   把字符串存到數組中
 *   由於存在對應關係,因此能夠考慮把每一個字符存進map集合中
 *   遍歷數組,若是出現字符就存
 *   由於要轉成相應的格式,因此能夠考慮StringBuilder
 *   
 *
 */
public class Test11 
{
	public static void main(String[] args)
	{
		String str = "asdasdasdada";
		
		char[] ch = str.toCharArray();
		
		TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
		
		for (int i=0;i<ch.length;i++)
		{
			Integer value = tm.get(ch[i]);
			
			if (value == null)
				tm.put(ch[i], 1);
			else
			{
				value += 1;
				tm.put(ch[i], value);
			}	
		}
		
		//經過StringBuilder轉換成相應格式
		StringBuilder sb = new StringBuilder();
		
		Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
		
		for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator(); it.hasNext(); )
		{
			Map.Entry<Character,Integer> me = it.next();
			
			Character key = me.getKey();			
			Integer value =me.getValue();
			
			sb.append(key+"("+value+")");
		}
		System.out.println(sb.toString());		
	}
}

package com.gotoheima;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * 已知文件a.txt文件中的內容爲「bcdeadferwplkou」,請編寫程序讀取該文件內容,並按照天然順序排序後輸出到b.txt文件中。即b.txt中的文件內容應爲「abcd…………..」這樣的順序。
 * 
 * 由於已知爲一個字符串,因此用字符流讀取,由於有重複字符,因此不可使用Set和Map集合
 * 因此存到ArrayList集合中,用collections中的sort方法進行排序
 * 寫入到b文件中輸出
 *
 */
public class Test12 
{
	public static void main(String[] args) throws IOException
	{
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try
		{
			fis = new FileInputStream("a.txt");
			fos = new FileOutputStream("b.txt");
		
			byte[] by = new byte[1024];
			int num = 0;
			while ((num = fis.read(by)) != -1)
			{
				Arrays.sort(by);
				fos.write(by,0,num);
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				fis.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
			try
			{
				fos.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}	
	}
}

package com.gotoheima;

/**
 * 編寫三各種Ticket、SealWindow、TicketSealCenter分別表明票信息、售票窗口、售票中心。售票中心分配必定數量的票,由若干個售票窗口進行出售,利用你所學的線程知識來模擬此售票過程。
 * 
 * 多個售票窗口一塊兒工做,這是一個多線程考題
 * 
 *
 */
public class Test13 
{
	public static void main(String[] args)
	{
		SealWindow sw = new SealWindow();
		
		Thread t1 = new Thread(sw);
		Thread t2 = new Thread(sw);
		Thread t3 = new Thread(sw);
		
		t1.start();
		t2.start();
		t3.start();
		
	}
}


class SealWindow implements Runnable
{
	TicketSealCenter tsc = new TicketSealCenter(100);
	
	private int ticket;
	
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"------"+ticket--);
	}
}

class TicketSealCenter
{
	private int ticket;
	
	TicketSealCenter(int ticket)
	{
		this.ticket = ticket;
	}

	public int getTicket() 
	{
		return ticket;
	}

	public void setTicket(int ticket)
	{
		this.ticket = ticket;
	}	
}

class Ticket
{
	private String name;//車次
	private int num;//座位號
	
	public String getName() 
	{
		return name;
	}
	
	public int getNum() 
	{
		return num;
	}
}

 

package com.gotoheima;

/**
 * 
 * 已知一個int類型的數組,用冒泡排序法將數組中的元素進行升序排列。
 *
 */
public class Test14 
{
	public static void main(String[] args)
	{
		int[] arr = {3,5,6,1,3,8,9,3,9};
		
		for (int x=0;x<arr.length-1;x++)
		{
			for (int y=0;y<arr.length-x-1;y++)
			{
				if (arr[y] > arr[y+1])
				{
					swap(arr,y,y+1);
				}
			}
		}
		for (int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]);
		}
	}
	
	public static void swap(int[] arr,int x,int y)
	{
		int temp = arr[x];
		arr[x] = arr[y];
		arr[y] = temp;
	}
}


 

package com.gotoheima;

/**
 * 假如咱們在開發一個系統時須要對員工進行建模,員工包含 3 個屬性:姓名、工號以及工資。經理也是員工,除了含有員工的屬性外,另爲還有一個獎金屬性。
 * 請使用繼承的思想設計出員工類和經理類。要求類中提供必要的方法進行屬性訪問。
 * 
 *
 */
public class Test15
{
	public static void main(String[] args)
	{
		Worker e = new Worker("leilei","007",5000);  
	    Manager m = new Manager("hanmeimei","001",10000,20000);  
	      
	    System.out.println("員工姓名:"+e.getName()+"\t工號:"+e.getId()+"\t工資:"+e.getMoney());  
	    System.out.println("經理姓名:"+m.getName()+"\t工號:"+m.getId()+"\t工資:"+m.getMoney()+"\t獎金:"+m.getBouns());  
		}
}

class Worker
{
	private String name;
	private String id;
	private long money;
	
	Worker(String name,String id,long money)
	{
		this.name = name;
		this.id = id;
		this.money = money;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public long getMoney() {
		return money;
	}

	public void setMoney(long money) {
		this.money = money;
	}
}

class Manager extends Worker
{
	private double bonus;
	
	Manager(String name,String id,long money,double bouns)
	{
		super(name, id, money);
		this.bonus = bouns;
	}

	public double getBouns() {
		return bonus;
	}

	public void setBouns(double bouns) {
		this.bonus = bouns;
	}
}



 

package com.gotoheima;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * 編寫程序,生成5個1至10之間的隨機整數,存入一個List集合,編寫方法對List集合進行排序(自定義排序算法,禁用Collections.sort方法和TreeSet),而後遍歷集合輸出。
 * 
 *
 */
public class Test16 
{
	public static void main(String[] args)
	{
		ArrayList<Integer> al = new ArrayList<Integer>();
		Random r= new Random();
		
		while (al.size()<5)
		{
			al.add(r.nextInt(10)+1);
		}
		
		//排序
		for (int x=0;x<al.size()-1;x++)
		{
			for (int y=0;y<al.size()-x-1;y++)
			{
				if (al.get(y)>al.get(y+1))
					Collections.swap(al, y, y+1);
			}
		}
		
		System.out.println(al);
	}
}


 

package com.gotoheima;

import java.util.Arrays;

/**
 * 把如下IP存入一個txt文件,編寫程序把這些IP按數值大小,從小到達排序並打印出來。
 * 61.54.231.245
 * 61.54.231.9
 * 61.54.231.246
 * 61.54.231.48
 * 61.53.231.249
 * 
 *
 */
public class Test17
{
	public static void main(String[] args)
	{
		String[] ip = {"61.54.231.245","61.54.231.9","61.54.231.246","61.54.231.48","61.53.231.249"};
		Arrays.sort(ip);
		
		for (int i=0;i<ip.length;i++)
		{
			System.out.println(ip[i]);
		}
	}
}

package com.gotoheima;

/**
 * 
 * 寫一方法,打印等長的二維數組,要求從1開始的天然數由方陣的最外圈向內螺旋方式地順序排列。 如: n = 4 則打印:
 * 1	2	3	4
 * 12	13	14	5
 * 11	16	15	6
 * 10	9	8	7
 *
 */
public class Test18 
{
	public static void main(String[] args)
	{
		printMath(60);
	}
	
	public static void printMath(int n)
	{
		int[][] arr = new int[n][n];
		int x = 0;
		int y = 0;
		int max = arr.length-1;
		int min = 0;
		int num = 1;
		
		while (min<=max)
		{
			//right
			while (y<max)
			{
				arr[x][y++] = num++;
			}
			//down
			while (x<max)
			{
				arr[x++][y] = num++;
			}
			//left
			while (y>min)
			{
				arr[x][y--] = num++;
			}
			//up
			while (x>min)
			{
				arr[x--][y] = num++;
			}
			//若是是奇數,可能賦值不上
			if (min == max)
			{
				arr[x][y] = num;
			}
			
			max--;
			min++;
			
			x++;
			y++;
		}
		
		for (int i=0;i<arr.length;i++)
		{
			for (int j=0;j<arr.length;j++)
			{
				System.out.print(arr[i][j]+"\t");
			}
			System.out.println();
		}
	}
}


 

package com.gotoheima;

/**
 *  28人買可樂喝,3個可樂瓶蓋能夠換一瓶可樂,那麼要買多少瓶可樂,夠28人喝?假如是50人,又須要買多少瓶可樂?(需寫出分析思路)
 * 	
 * 		三個瓶蓋就是一個可樂和一個瓶蓋
 *
 */
public class Test19 
{
	public static void main(String[] args)
	{
		System.out.println(buyCoke(28));
		System.out.println(buyCoke(50));
	}
	
	public static int buyCoke(int num)
	{
		int coke = 0;
		int cap = 0;
		int buy = 0;
		
		while (coke < num)
		{
			buy++;
			coke++;
			cap++;
			
			if (cap == 3)
			{
				coke++;
				cap = 1;
			}
		}
		return buy;
	}
}


 

package com.gotoheima;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 自定義字符輸入流的包裝類,經過這個包裝類對底層字符輸入流進行包裝,讓程序經過這個包裝類讀取某個文本文件(例如,一個java源文件)時,可以在讀取的每行前面都加上有行號和冒號。
 *
 */
public class Test20 
{
	public static void main(String[] args)
	{
		MyLineNumberReader mlnr = null;
		
		try
		{
			mlnr = new MyLineNumberReader(new FileReader("F:\\java\\day09\\Demo.java"));
			
			String len = null;
			
			while ((len = mlnr.myReadLine()) != null)
			{
				System.out.println(mlnr.getCount()+":\t"+len);
			}
		}
		catch (IOException e)
		{
			System.out.println("讀取出錯!");
		}
		finally
		{
			try
			{
				if (mlnr != null)
					mlnr.myclose();
			}
			catch (IOException e)
			{
				System.out.println("關閉出錯");
			}
		}
	}
}

class MyLineNumberReader
{
	private Reader r;
	private int count;

	MyLineNumberReader(Reader r)
	{
		this.r = r;
	}
	
	public String myReadLine() throws IOException
	{
		StringBuffer sb = new StringBuffer();
		
		int num = 0;
		count++;
		
		while ((num = r.read()) != -1)
		{
			if (num == '\r')
				continue;
			if (num == '\n')
				return sb.toString();
			else
				sb.append((char)num);
		}
		if (sb.length() != 0)
			return sb.toString();
		return null;
	}
	
	public void myclose() throws IOException
	{
		r.close();
	}
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}


 

package com.gotoheima;

/**
 * 使用TCP協議寫一個能夠上傳文件的服務器和客戶端。
 * 
 *
 */
public class Test21 
{
	public static void main(String[] args)
	{
		
	}
}


 

import java.io.*;
import java.util.*;

/**
	把一個文件夾下的包括子文件夾裏的全部.java文件複製到另外一個文件夾下面,並更名.txt

	對一個文件夾進行遞歸,篩選出.java文件,並存到集合中
	用IO字符流操做複製文件,並將.java文件改爲.txt
*/

class DirPrintListDemo 
{
	public static void main(String[] args) throws IOException
	{
		//源文件夾
		File dir = new File("F:\\java");

		//目標文件夾
		File copy = new File("F:\\copy_java");
		if (!copy.exists())
			copy.mkdir();

		ArrayList<File> dirlist = new ArrayList<File>();
		fileToArray(dir,dirlist);
		copy_ReName(dirlist,copy);
		
	}

	//函數功能,把一個文件夾中包括子文件夾的全部.java文件存到dirlist集合中
	public static void fileToArray(File dir,List<File> dirlist)
	{
		File[] files = dir.listFiles();

		for (File file : files )
		{
			if (file.isDirectory())
				fileToArray(file,dirlist);
			else
			{
				if (file.getName().endsWith(".java"))
					dirlist.add(file);
			}
		}
	}

	//函數功能,用IO字符流操做複製文件,並將.java文件改爲.txt
	public static void copy_ReName(List<File> dirlist,File copy) throws IOException
	{
		//遍歷集合
		for (File files : dirlist)
		{
			BufferedReader br = new BufferedReader(new FileReader(files));
			String newName = files.getName().replace(".java",".txt");
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(copy,newName)));

			String len = null;
			while ((len = br.readLine()) != null)
			{
				bw.write(len);
				bw.newLine();
				bw.flush();
			}

			br.close();
			bw.close();
		}
	}
}


 

 

 

package com.gotoheima;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 編寫一個類,在main方法中定義一個Map對象(採用泛型),加入若干個對象,而後遍歷並打印出各元素的key和value。
 * 
 *	
 */
public class Test22 
{
	public static void main(String[] args)
	{
		Map<String,Integer> m = new TreeMap<String,Integer>();
		m.put("Lilei", 10);
		m.put("hanmeimei", 9);
		
		//第一種
		Set<Map.Entry<String,Integer>> entrySet = m.entrySet();
		
		for (Iterator<Map.Entry<String,Integer>> it = entrySet.iterator();it.hasNext(); )
		{
			Map.Entry<String,Integer> me = it.next();
			
			String key = me.getKey();
			Integer value = me.getValue();
			System.out.println(key+"-------"+value);
		}
		
		//第二種
		Set<String> keySet = m.keySet();
		
		for (Iterator<String> it = keySet.iterator();it.hasNext(); )
		{
			String key = it.next();
			
			Integer value = m.get(key);
			System.out.println(key+"-------"+value);
		}
	}
}


 

package com.gotoheima;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 把當前文件中的全部文本拷貝,存入一個txt文件,統計每一個字符出現的次數並輸出,例如:
  a: 21 次 
  b: 15 次
  c:: 15 次
  把: 7 次
  當: 9 次
  前: 3 次
  ,:30 次
 * 
 *
 */
public class Test23 
{
	public static void main(String[] args) throws IOException
	{
		    BufferedReader br = new BufferedReader(new FileReader("面試題.txt"));
		    BufferedWriter bw = new BufferedWriter(new FileWriter("tongji.txt"));
			TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
			
			String len = null;
			while ((len = br.readLine()) != null)
			{
				char[] chs = len.toCharArray();
				
				for (int i=0;i<chs.length;i++)
				{
					Integer value = tm.get(chs[i]);
					if (value == null)
					{
						tm.put(chs[i],1);
					}
					else
					{
						value += 1;
						tm.put(chs[i], value);
					}
				}
			}
	
			Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
			for (Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();it.hasNext(); )
			{
				Map.Entry<Character, Integer> me = it.next();
				Character key = me.getKey();
				Integer value = me.getValue();
				
				bw.write(key+": "+value+"次");
				bw.newLine();
				bw.flush();
			}
			br.close();
			bw.close();
	}
}


 

package com.gotoheima;

/**
 * 在一個類中編寫一個方法,這個方法搜索一個字符數組中是否存在某個字符,若是存在,則返回這個字符在字符數組中第一次出現的位置(序號從0開始計算),不然,返回-1。
 * 要搜索的字符數組和字符都以參數形式傳遞傳遞給該方法,若是傳入的數組爲null, 應拋出IllegalArgumentException異常。在類的main方法中以各類可能出現的狀況測試驗證該方法編寫得是否正確,
 *  例如,字符不存在,字符存在,傳入的數組爲null等。
 * 
 *
 */
public class Test24 
{
	public static void main(String[] args)
	{
		char[] chs = {'z','a','c','t','c'};
		
		IsTest it = new IsTest();
		
		System.out.println(it.isTest(chs, 'c'));
		System.out.println(it.isTest(chs, 'v'));
		System.out.println(it.isTest(null, 'c'));
		
	}
}

class IsTest
{
	public int isTest(char[] chs,char ch)
	{
		if (chs == null)
			throw new IllegalArgumentException("傳入字符數組不能爲空!");
		
		for (int i=0;i<chs.length;i++)
		{
			if (ch == chs[i])//基本數據類型不能用equals,用==
				return i;
		}
		return -1;
	}
}


 

package com.gotoheima;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 已知一個類,定義以下:
  package cn.itcast.heima;
  public class DemoClass {
    public void run()
    {
      System.out.println("welcome to heima!");
    } 
  }
  (1) 寫一個Properties格式的配置文件,配置類的完整名稱。
  (2) 寫一個程序,讀取這個Properties配置文件,得到類的完整名稱並加載這個類,用反射 的方式運行run方法。
 * 
 *
 */
public class Test25 
{
	public static void main(String[] args) throws Exception
	{
		Properties prop = new Properties();
		prop.load(new FileReader("cn\\itcast\\heima\\class.propertise"));
		
		String className = prop.getProperty("name");
		Class clazz = Class.forName(className);
		Method method = clazz.getMethod("run", null);
		method.invoke(clazz.newInstance());
	}
}


 

package com.gotoheima;

import java.util.Scanner;

/**
 * 金額轉換,阿拉伯數字轉換成中國傳統形式。例如:101000001010 轉換爲壹仟零壹拾億零壹仟零壹拾圓整
 * 
 * 掃描一個數轉換字符串存進一個數組中
 * 定義兩個字符數組,分別是零壹貳叄肆伍陸柒捌玖,圓拾佰扦萬拾萬佰萬仟萬億拾億佰億仟億萬億
 *
 */
public class Test26 
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("請輸入金額:");
		String str = sc.nextLine();
		
		System.out.println(getChinese(str));
		
		
	}
	
	public static String getChinese(String str)
	{
		String[] str1 = {"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
		String[] str2 = {"整圓","拾","佰","扦","萬","拾萬","佰萬","仟萬","億","拾億","佰億","仟億","萬億"};
		StringBuilder sb = new StringBuilder();
		
		char[] chs = str.toCharArray();
	
		for (int i=chs.length-1;i>=0;i--)
		{
			
		}
		
		return sb.toString();
	}
}


 

package com.gotoheima;

/**
 * 方法中的內部類能不能訪問方法中的局部變量,爲何?
 * 
 * 	由於內部類的生存週期比方法中的局部變量長,局部變量再做用域完成後就會被棧內存釋放銷燬。要想訪問局部變量,那麼局部變量必須被final修飾。
 *
 */
public class Test27 {

}


 

package com.gotoheima;

/**
 * 有一個類爲ClassA,有一個類爲ClassB,在ClassB中有一個方法b,此方法拋出異常,在ClassA類中有一個方法a,
 * 請在這個方法中調用b,而後拋出異常。在客戶端有一個類爲TestC,有一個方法爲c ,請在這個方法中捕捉異常的信息。
 * 完成這個例子,請說出java中針對異常的處理機制。
 * 
 * java中的異常處理機制,誰調用誰處理,若是一直拋出最終會拋給虛擬機
 *
 */
public class Test28 
{
	public static void main(String[] args)
	{
		C.c();
	}
}

class A
{
	public static void a() throws Exception
	{
		B.b();
	}
}	

class B
{
	public static void b() throws Exception
	{
		throw new Exception("b");
	}
} 

class C
{
	public static void c()
	{
		try
		{
			A.a();
		}
		catch(Exception e)
		{
			System.out.println("處理");
		}
	}
}


 

package com.gotoheima;

import java.lang.reflect.Field;

/**
 * 寫一個方法,此方法可將obj對象中名爲propertyName的屬性的值設置爲value. 
 * 
 * public void setProperty(Object obj, String propertyName, Object value) 
 *   {    
 *   } 
 *   
 *	考點是反射
 */
public class Test29
{
	public static void main(String[] args)
	{
		Person p = new Person();  
	    System.out.println(p.name+"---"+p.age); 
	    
	    setProperty(p,"name","Hanmeimei");  
        setProperty(p,"age",19);  
        System.out.println(p.name+"---"+p.age);  
	}
	
	public static void setProperty(Object obj,String propertyName,Object value) 
	{
		try
		{
			Field f = obj.getClass().getDeclaredField(propertyName);
			
			f.setAccessible(true);
			
			f.set(obj, value);
			
			f.setAccessible(false);
		}
		catch (Exception e)
		{
			System.out.println("修改錯誤");
		}
	}
	
	public static class Person
	{
		public String name = "Lilei";
		private int age = 18;
	}
}


 

package com.gotoheima;

import java.util.LinkedList;

/**
 * 有100我的圍成一個圈,從1開始報數,報到14的這我的就要退出。而後其餘人從新開始,從1報數,到14退出。問:最後剩下的是100人中的第幾我的?
 *
 */
public class Test30 
{
	public static void main(String[] args)
	{
		final int num = 14;
		int count = -1;
		
		LinkedList<Integer> ll = new LinkedList<Integer>();
		
		for (int i=0;i<100;i++)
		{
			ll.add(i+1);
		}
		
		while (ll.size() != 1)
		{
			for (int x=1;x<=num;x++)
			{
				count ++;
				if (count>=ll.size())
					count = 0;
			}
			ll.remove(count);
			count--;
		}
		System.out.print(ll.get(0));
	}
}
相關文章
相關標籤/搜索