這一個星期學到的有:異常,集合,IO流,多線程。編程
1.我我的理解異常就是在程序運行的過程當中發生一些不正常的事件,它會結束正在運行的程序。
2.Java異常處理:Java它有對異常處理的能力,Java有5個關鍵字來處理異常:try、catch、finally、throw、throws
try:它執行可能產生異常的代碼
catch:它是用來捕獲異常的
finally:它是不管程序出不出現異常,它都會執行的代碼
throw:自動拋出異常和手動拋出異常
throws:聲明方法可能要拋出的各類異常多線程
public static void main(String[] args) { // TODO Auto-generated method stub // 異常 try { System.out.print("請輸入第一個數:"); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); System.out.print("請輸入第一個數:"); int b = scanner.nextInt(); System.out.println(a / b); } catch (InputMismatchException exception) { System.out.println("除數和被除數必須爲正數"); } catch (ArithmeticException exception) { System.out.println("除數不能爲0"); } catch (Exception e) { System.out.println("其它異常"); } finally { System.out.println("謝謝使用"); } }
public static void main(String[] args) { // TODO Auto-generated method stub try { Scanner scanner = new Scanner(System.in); System.out.print("請輸入課程代號(1~3之間的數字):"); int a = scanner.nextInt(); switch (a) { case 1: System.out.println("C#編程"); break; case 2: System.out.println("C語言"); break; case 3: System.out.println("JAVA"); break; default: System.out.println("只能輸入1~3之間的數字"); break; } } catch (InputMismatchException exception) { System.out.println("輸入的數字必須爲數字"); } finally { System.out.println("歡迎提出建議"); } }
public static void main(String[] args) { // TODO Auto-generated method stub try { Person person = new Person(); person.setAge(10); person.setSex("無"); System.out.println("建立成功"); } catch (Exception e) { System.err.println(e.getMessage()); } finally { System.out.println("人是一個高等級動物"); } } } class Person { private int age; private String sex; // public int getAge() { return age; } //throws Exception是一個異常拋出的管道 public void setAge(int age) throws Exception { if (age > 0) { this.age = age; } else { throw new Exception("年齡必須大於0"); //建立的異常對象 } } public String getSex() { return sex; } public void setSex(String sex) throws Exception { if ("男".equals(sex) || "女".equals(sex)) { this.sex = sex; } else { throw new Exception("必須是男或女"); } }
這些是我作的練習併發
1.collection集合:list 、set
2.list: arrayList 、LinkedList
3.arrayListapp
public class Demo01 { public static void main(String[] args) { // TODO Auto-generated method stub List list = new ArrayList(); list.add(new Dog("強強", 12)); list.add(new Dog("球球", 12)); list.add(new Dog("希希", 12)); list.add(1, new Dog("安安", 11)); Dog dog2 = new Dog("大大", 13); list.add(dog2); // 刪除 // E remove(int index) // 移除此列表中指定位置上的元素。 // boolean remove(Object o) // 移除此列表中首次出現的指定元素。 list.remove(0); list.remove(dog2); // list.clear();// 移除所有 // 查詢 list.set(1, new Dog("麗麗", 12)); for (int i = 0; i < list.size(); i++) { // get是索取某個索引位置上的內容 Dog dog = (Dog) list.get(i); System.out.println(dog.toString()); } } }
public class Demo01 { public static void main(String[] args) { // TODO Auto-generated method stub LinkedList list = new LinkedList();// 父類引用指向子類對象 // 建立對象 Dog pingping = new Dog("平平", 2); Dog tutu = new Dog("兔兔", 2); Dog tu2 = new Dog("圖圖", 2); // 添加到集合中 list.add(pingping); list.addFirst(tutu); list.addLast(tu2); // 得到第一個元素 System.out.println(list.getFirst().toString()); System.out.println(list.getLast().toString()); // 刪除第一個 list.removeFirst(); list.removeLast(); System.out.println("~~~~~~~~~~~~"); for (int i = 0; i < list.size(); i++) { Dog dog = (Dog) list.get(i); System.out.println(dog.toString()); } } }
4.set:hashsetdom
public class Demo04 { public static void main(String[] args) { // TODO Auto-generated method stub Set<Integer> set = new HashSet<Integer>(); Random random = new Random(); while (set.size() <= 10) { set.add(random.nextInt(20) + 1); } for (Integer integer : set) { System.out.println(integer); } } }
public class Demo05 { public static void main(String[] args) { // TODO Auto-generated method stub //去除重複的字符 Scanner scanner = new Scanner(System.in); System.out.print("請輸入一段字符:"); String aString = scanner.nextLine(); System.out.println(aString); HashSet<Character> hs = new HashSet<Character>(); char[] arr = aString.toCharArray(); for (char c : arr) { hs.add(c); } for (Character ch : hs) { System.out.print(ch); } } }
泛型就是容許在編寫集合的時候,先限制集合的數據處理類型ide
map:將鍵映射到值的對象,一個映射不能包含重複的鍵;每一個鍵最多隻能映射到一個值this
public static void main(String[] args) { // TODO Auto-generated method stub Map<Character, Integer> map = new HashMap(); Scanner scanner = new Scanner(System.in); System.out.print("請輸入一段字符串:"); String a = scanner.next(); char[] chars = a.toCharArray(); for (char c : chars) { if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } for (Character character : map.keySet()) { System.out.println(character + ":" + map.get(character)); } }
list遍歷能夠用for循環也能夠用foreach
set只能用foreach線程
public static void main(String[] args) { // TODO Auto-generated method stub Map<String, String> map = new HashMap<String, String>(); // put方法若是集合當中沒有對應鍵值則新增,若是有則修改; map.put("YL", "yl"); map.put("LY", "ly"); map.put("XY", "xy"); /* * //第一種迭代器 //獲取全部的鍵 而且存放到set集合中; Set<String> set = map.keySet(); * //set集合的迭代器; Iterator<String> iterator = set.iterator(); * //首先判斷set集合中是否有下一個元素; //若是有則獲取下一個元素; while (iterator.hasNext()) { * //next:獲取當前指針指向的對象的引用,得到以後,本身把指針向後移一位; String iString = * iterator.next(); System.out.println(iString + "\t" + * map.get(iString)); } */ // 第二種迭代器 Set<Map.Entry<String, String>> set2 = map.entrySet(); for (Map.Entry<String, String> entry : set2) { System.out.println(entry); } }
1.file類:就是文件在系統當中具體的位置
2.建立文件和文件夾指針
public static void main(String[] args) { // TODO Auto-generated method stub // Demo01(); // Demo02(); File file = new File("E:\\JAVA\\新建文件夾"); if (file.mkdirs()) { System.out.println("建立成功"); } else { System.out.println("建立失敗"); } } private static void Demo02() { File file = new File("E:\\JAVA\\新建文件夾\\abd"); if (file.mkdir()) { System.out.println("建立成功"); } else { System.out.println("建立失敗"); } } // 建立空的文件 private static void Demo01() { File file = new File("E:\\JAVA\\新建文件夾\\abd.txt"); try { // file.createNewFile(); if (file.createNewFile()) { System.out.println("建立成功"); } else { System.out.println("建立失敗"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3.重命名和刪除code
public static void main(String[] args) { // Demo01(); File newfile = new File("E:\\JAVA\\新建文件夾\\abd2"); if (newfile.delete()) { System.out.println("刪除成功"); } } // 修改 private static void Demo01() { File file = new File("E:\\JAVA\\新建文件夾\\abd"); // 新的路徑 File newfile = new File("E:\\JAVA\\新建文件夾\\abd2"); if (file.renameTo(newfile)) { System.out.println("修改爲功"); } else { System.out.println("修改失敗"); } }
1.io流:Java對數據的讀寫(傳輸)操做經過流的方式
2.字節流:能夠操做任何數據
3.字符流:用於操做純字符文件
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // 建立輸入流 FileInputStream fileInputStream = new FileInputStream( "E:\\JAVA\\新建文件夾\\一輩子所愛.flac"); // 二、建立輸出流 FileOutputStream fStream = new FileOutputStream("E:\\JAVA\\新建文件夾\\一輩子所愛2.flac"); // 三、讀寫 int b; while ((b = fileInputStream.read()) != -1) { fStream.write(b); } fileInputStream.close(); fStream.close(); }
public static void main(String[] args) { // TODO Auto-generated method stub FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader("E:\\JAVA\\IO流\\愛你.txt"); fileWriter = new FileWriter("E:\\JAVA\\IO流\\愛你愛你.txt"); int c; while ((c = fileReader.read()) != -1) { fileWriter.write(c); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fileWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader bf = null; BufferedWriter bw = null; try { bf = new BufferedReader(new FileReader("E:\\JAVA\\IO流\\abc.txt")); String line = null; StringBuffer stringBuffer = new StringBuffer(); while ((line = bf.readLine()) != null) { stringBuffer.append(line); } System.out.println("替換前:" + stringBuffer); bw = new BufferedWriter(new FileWriter("E:\\JAVA\\IO流\\abc.txt")); // 替換字符 String string = stringBuffer.toString().replace("name", "XL").replace("type", "哈士奇").replace("master", "AL"); bw.write(string); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { try { bf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
1.多線程:程序執行的多個路徑
2.多線程併發:能夠提升程序的執行效率,能夠同時完成多個任務
3.多線程的運行:多個程序同時在運行
4.實現多線程運行的方式:1.子類繼承thread類,重寫run方法。當執行start方法時,直接執行子類的run方法 2.實現runnable接口
public static void main(String[] args) { // TODO Auto-generated method stub Mythread mythread = new Mythread(); Mythread mythread2 = new Mythread(); mythread.setName("你好,來自線程" + mythread); mythread.start(); mythread2.setName("你好,來自線程" + mythread2); mythread2.start(); } } class Mythread extends Thread { public void run() { for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } }
休眠線程
public static void main(String[] args) { // TODO Auto-generated method stub // DemoA(); for (int i = 10; i > 0; i--) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("敵軍還有:" + i + "S到達戰場"); } System.out.println("全軍出擊"); } // ******************************************************************** private static void DemoA() { new Thread() { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(1500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); new Thread() { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(1500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } }
線程的優先級
public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread(new MyThread2(), "線程A"); Thread thread2 = new Thread(new MyThread2(), "線程B"); // 設置線程的優先級 thread.setPriority(thread.MAX_PRIORITY); thread2.setPriority(thread.MIN_PRIORITY); thread.start(); thread2.start(); System.out.println(thread.getPriority()); System.out.println(thread2.getPriority()); } } class MyThread2 implements Runnable { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "~~~~~~" + i); } } }
接口實現線程
public static void main(String[] args) { // TODO Auto-generated method stub // 建立線程對象 MyRunnable myRunnable = new MyRunnable(); // 開啓線程 Thread thread = new Thread(myRunnable); Thread thread2 = new Thread(myRunnable); thread.start(); thread2.start(); } } class MyRunnable implements Runnable { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } }
多線程執行狀態
public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread(new MyThread3()); System.out.println("線程已建立"); thread.start(); System.out.println("線程就緒"); } } class MyThread3 implements Runnable { @Override public void run() { // TODO Auto-generated method stub System.out.println("程序在運行"); try { System.out.println("立刻進入休眠期"); Thread.sleep(1000); System.out.println("通過短暫的休眠以後"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
線程的禮讓
public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread(new Mythread5(), "線程A"); Thread thread2 = new Thread(new Mythread5(), "線程B"); thread.start(); thread2.start(); } } class MyThread5 implements Runnable { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + "正在運行"); if (i == 3) { System.out.println("線程的禮讓"); Thread.yield(); } } }
多線程練習題
public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread(new MyThread4()); thread.setPriority(10); thread.start(); for (int i = 1; i < 50; i++) { System.out.println("普通號:" + i + "號病人在看病"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (i == 10) { try { thread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } class MyThread4 implements Runnable { // 特需號 @Override public void run() { // TODO Auto-generated method stub for (int i = 1; i < 11; i++) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("特需號:" + i + "號病人在看病!"); } } } 這些就是一星期的總結