日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架

發布時間:2024/8/26 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

大家好,給大家帶來詳細講解Java中的異常處理情況與I/O流的介紹以及類集合框架的概述,希望你們喜歡

JAVA 異常

try...catch...finally結構的使用方法

class Test{ public static void main(String args[]){try{ int i = 1 / 0;}catch(Exception e){e.printStackTrace();}finally{System.out.println("finally");}System.out.println(5);} }class Test{ public static void main(String args[]){try{Thread.sleep(1000);}catch(Exception e){e.printStackTrace();} } }

throw和throws的作用區別:

class Person{ private int age;public void setAge(int age) throws Exception{if(age<0){RuntimeException e = new RuntimeException("年齡不能小于0");throw e;}this.age = age;} }class Test{ public static void main(String args[]){Person person = new Person();try{person.setAge(-1);}catch(Exception e){System.out.println(e);} } }

Error和Exception的區別

  • Error是Throwable的子類用于標記嚴重錯誤
  • Exception是Throwable的子類,指示合理的程序想去catch的條件,非嚴重錯誤。

    try/catch的執行過程

    如果出現異常,系統則會拋出一個異常,進行捕捉(catch操作),或在最后(finally)來進行處理。

    throw和throws的區別

    throws 出現在方法聲明上,throw出現在方法體內。

    異常分類

    異常分類:可查異常,運行時異常和錯誤

    說說IO

//第一種:輸入流輸出流 //第二種:字節流字符流 //第三種:節點流處理流 //FileInputStream class Test{public static void main(String args[]){FileInputStream fis = null;try{fis = new FileInputStream("e:/read.txt");byte[] buffer = new byte[100];fis.read(buffer,0,buffer.length);for(int i = 0;i<buffer.length;i++){System.out.println(buffer[i]);} }catch(Exception e){System.out.println(e);}} } class Test{public static void main(String args[]){FileInputStream fis = null;FileOutputStream fos = null;try{fis = new FileInputStream("e:/read.txt");fos = new FileOutputStream("e:/write.txt");byte[] buffer = new byte[100];int temp = fis.read(buffer,0,buffer.length);fos.write(buffer,0,temp);}catch(Exception e){System.out.println(e);}} } class Test{public static void main(String args[]){FileInputStream fis = null;FileOutputStream fos = null;try{fis = new FileInputStream("e:/read.txt");fos = new FileOutputStream("e:/write.txt");byte[] buffer = new byte[1024];while(true){int temp = fis.read(buffer,o,buffer.length);if(temp = -1){break;}fos.write(buffer,0,temp);}}catch(Exception e){System.out.println(e);}finally{try{fis.close();fos.close();}catch(Excepiton e){System.out.println(e);}} } } //字符流 public class TextCharpublic static void main(String args[]){FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("e:/read.txt");fw = new FileWriter("e:/write.txt");char[] buffer = new char[100];int temp = fr.read(buffer,0,buffer.length);fw.write(buffer,0,temp); }catch(Exception e){System.out.println(e);}finally{try{fr.close();fw.close();}catch(Excepiton e){System.out.println(e); }} } //FileReader和BufferedReader class Test{public static void main(String args[]){FileReader fileReader = null;BufferedReader bufferedReader = null; try{fileReader = new FileReader("e:/read.txt");bufferedReader = new BufferedReader(fileReader);String line = null;while(true){line = bufferedReader.readLine();if(line == null){ break;}System.out.println(line);}}catch(Exception e){System.out.println(e); }finally{try{bufferedReader.close(); fileReader.close();}catch(Exception e){System.out.println(e);}}} } public class Test{public static void main(String[] args) throws Exception{//字節流FileInputStream in = new FileInputStream("c:/read.txt");FileOutStream out = new FileOutputStream("c:/write.txt");byte[] buffer = new byte[1024];int len;while( (len = in.read(buffer)) != -1){out.write(buffer,0,len);}in.close();out.close();//字符流BufferedReader bf = new BufferedReader(new FileReader("c:/read.txt");BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt");String str;while( (str=bf.readLine()) != null ){bw.write(str);bw.newLine();}bf.close();bw.close();} }
  • 字節流: InputStream字節輸入流,OutputStream字節輸出流
  • 字符流 : Reader字符輸入流 ,Writer字符輸出流
  • 數據流: DataInputStream 數據輸入流 ,DataOutputStream 數據輸出流

集合框架

一組類和接口,位于java.util包,主要用于存儲和管理對象,主要分為三大類---集合,列表和映射。

什么是集合(Set)
集合中對象是沒有順序的,并且沒有重復對象;

什么是列表(List)
集合中對象可以有重復的對象,可以按照順序取,也可以指定取。

什么是映射(Map)
每一個元素包含一個鍵對象和一個值對象,鍵不可以重復,值可以重復。

類集框架主體結構

interface Iterator Collection ListIterator List Set Map LinkeList ArrayList HashSet SortedSet HashMap SortedMap LinkedHashSet TreeSet LinkedHashMap TreeMap Comparable Comparator Collections Arrays//arrayList默認10,可無限長,關于泛型public class Test{ public static void main(String args[]){//ArrayList arrayList = new ArrayList();ArrayList<String> arrayList = new ArrayList<String>();arrayList.add("a");arrayList.add("b");arrayList.add("c");//String s = arrayList.get(1);//System.out.println(s);for(int i=0;i<3;i++){String s = arrayList.get(i);System.out.println(s);}} }優化public class Test{ public static void main(String args[]){ArrayList<String> arrayList = new ArrayList<String>();arrayList.add("a");arrayList.add("b");arrayList.add("c");arrayList.add("d");for(int i = 0; i<arrayList.size();i++){String s = arrayList.get(i);System.out.println(s);} } }

類集框架

集合 無序 不可重復
列表 有序 可重復
映射

Set繼承了Collection

public class Test{public static void main(String args[]){//HashSet<String> hashSet = new HashSet<String>();//Set<String> set = new HashSet<String>();//別管就是轉,方便Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");int i = set.size();System.out.println(i);} }不可以重復public class Test{public static void main(String args[]){//HashSet<String> hashSet = new HashSet<String>();//Set<String> set = new HashSet<String>();//別管就是轉,方便Set<String> set = new HashSet<String>();boolean b1 = set.isEmpty();System.out.println(b1);set.add("a");set.add("b");set.add("c");set.add("d");set.add("c");boolean b2 = set.isEmpty();System.out.println(b2);int i = set.size();System.out.println("clear之前的長度"+i);set.clear();int j = set.size();System.out.println(j);} }取數據,迭代 iterate器 (Iterator)public class Test{ public static void main(String args[]){//HashSet<String> hashSet = new HashSet<String>();//Set<String> set = hashSet;//Iterator <-- Collection <-- Set <-- HashSet//hasNext() next()Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");set.add("c");Iterator<String> it = set.iterator();boolean b1 = it.hasNext();if(b1){String s = it.next();System.out.println(s);}boolean b2 = it.hasNext();if(b2){String s = it.next();System.out.println(s);}} }迭代器的使用 it.hasNext(); 還有沒有下一個元素,如果這個游標后面有元素就返回true,否則,false;it.next(); 返回游標所指位置的下一個元素,取出,用hasNext()看有沒有,next取優化 public class Test{ public stattic void main(String args[]){Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");set.add("c");Iterator<String> it = set.iterator();while(it.hasNext()){String s = it.next();System.out.println(s); } } }

什么是映射(Map)
每一個元素包含一個鍵對象和一個值對象,鍵不可以重復,值可以重復。

public class Test{ public static void main(String args[]){HashMap<String,String> hasMap = new HashMap<String,String>();Map<String,String> map = hasMap;map.put("1","a");map.put("2","b");map.put("3","c");map.put("4","d");int i = map.size();System.out.println(i); } }public class Test{ public static void main(String args[]){HashMap<String,String> hasMap = new HashMap<String,String>();Map<String,String> map = hasMap;map.put("1","a");map.put("2","b");map.put("3","c");map.put("4","d");map.put("3","e");int i = map.size();System.out.println(i);String s = map.get("3");System.out.println(ss); } } public class TestCollection {public static void main(String[] args) {List<Hero> heros = new ArrayList<Hero>();for (int i = 0; i < 5; i++) {heros.add(new Hero("hero name " + i));}for (int i = 0; i < heros.size(); i++) {Hero h = heros.get(i);System.out.println(h);}} }

總結

  • 本文講了詳細講解Java中的異常處理情況與I/O流的介紹以及類集合框架,如果您還有更好地理解,歡迎溝通
  • 定位:分享 Android&Java知識點,有興趣可以繼續關注

轉載于:https://www.cnblogs.com/dashucoding/p/9264850.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。