20165330 2017-2018-2 《Java程序設計》第6周學習總結

課本知識總結

第八章 經常使用實用類

String類
  1. String對象
    • 構造方法:
  • 使用String類聲明對象並建立對象
String s = new String("we are students");
String t = new String("we are students");
  • String(char a[])用一個字符數組a建立一個String對象
char a[] = {'J','a','v','a'};
String s = new String(a);
  • String(char a[],int starIndex,int count)提取字符數組a中的一部分字符建立一個String對象,參數starIndex和count分別指定在a中提取字符的起始位置和從該位置開始截取的字符個數。
    • 引用String常量
String s1,s2;
s1 = "你好";
s2 = "你好";
  1. 字符串的並置
  • String對象能夠用「+」進行並置運算,即首尾相接獲得一個新的String對象
  1. String類的經常使用方法
  • public int length()
  • public boolean equals(String s)
  • public boolean startsWith(String s)、public boolean endsWith(String s)
  • public int compareTo(String s)方法
  • public boolean contains(string s)
  • public int indexOf(String s)和public int lastIndexOf(String s)
  • public String substring(int starpoint)
  • public String trim()
  1. 字符串與基本數據的相互轉化
  • java.lang包中的Integer類調用其類方法public static int parseInt(String s)能夠將「數字」字符組成的字符序列,如:
int x;
String s = "876";
x = Integer.parseInt(s);
  1. 字符串的替換
  • String對象調用public String replaceAll(String regex,String replacement)方法返回一個新的String對象
  1. 字符序列的分解
  • 利用方法public String[] split(String regex),使用regex做爲分隔標記分解出當前String對象的字符序列中的單詞
StringTokenizer類
  1. StringTokenizer(String s):爲String對象a構造一個分析器。使用默認的分隔標記,即空格符、換行符、回車符、Tab符、進紙符作分隔標記
  2. StringTokenizer(String s,String delim):爲String對象s構造一個分析器。參數delim的字符序列中的字符的任意排列被做爲分隔標記
Scanner類
  1. Scanner對象能夠解析字符序列中的單詞
String NBA = "I Love This Game";
Scanner scanner = new Scanner(NBA);
useDelimiter(正則表達式);
  1. 與StringTokenizer的區別
  • StringTokenizer類把分解出的所有單詞都存放到StringTokenizer對象的實體中
  • Scanner類僅存放怎樣獲取單詞的分隔標記
StringBuffer類
  1. 構造方法
  • StringBuffer();
  • StringBuffer(int size);
  • StringBuffer(String s);
  1. 經常使用方法
  • append方法
  • public char charAt(int n)和public void setCharAt(int n,char ch)
  • StringBuffer insert(int index,String str)
  • public StringBuffer delete(int startIndex,int endIndex)
  • public StringBuffer reverse()
  • StringBuffer replace(int startIndex,int endIndex,String str)
date類與Calendar類
  1. Date類
  • 構造方法
    • 無參數構造方法:Date nowTime = new Date();
    • 帶參數構造方法:Date date1=new Date(1000),date2=new Date(-1000);
  1. Calendar類
  • 初始化:Calendar calendar = Calendar。getInstance();
  • 調用方法public int get(int filed)能夠獲取有關年份、月份、小時、星期等信息
  • 調用public long getTimeInMillis()能夠返回當前calendar對象中時間的毫秒計時
日期的格式化
  1. format方法:format(格式化模式,日期列表)
  • 格式化模式:是一個用雙引號括起的字符序列,該字符序列中的字符由時間格式符和普通字符所構成
  • 日期列表:能夠是用逗號分隔的Calendar對象或Date對象
  • 格式化同一日期:可以使用「<」格式化同一個日期
  1. 不一樣區域的星期格式
  • 用特定地區的星期格式:format(Locale locale,格式化模式,日期列表);
  1. 數字格式化:調用format方法
  • 數據的寬度:"%md"在數字的左面增長空格;"%-md"在數字的右面增長空格
Class類和Console類
  1. Class類
  • 建立對象:
public static Class forName(String className) throws ClassNotFoundException
  • 對象調用:
public Object newInstance() throws InstantiationException,IllegalAccessException
  1. Console類
  • 建立對象:Console cons = System.console();
  • 讀取文本且不回顯:char[] passwd = cons.readPassword();
Pattern類與Matcher類
  1. 創建Pattern對象:使用正則表達式regex做參數獲得一個稱爲模式的Pattern類的實例pattern
Pattern pattern = Pattern.compile(regex);
  1. 獲得Macher對象:得能夠檢索String對象input的Macher類的實例macher(稱爲匹配對象)
Macher matcher = pattern.matcher(input);

第十五章 泛型與集合框架

  1. 泛型
  • 泛型類聲明:class 名稱 <泛型列表>
  • 聲明對象
Cone<Circle> coneOne;
coneOne = new Cone<Circle>(new Circle());
  1. 鏈表
  • LinkedList 鏈表:建立對象以鏈表結構存儲數據
  • 建立空鏈表:LinkedList mylist = new LinkedList ();
  • 使用add(E obj)方法依次增長節點
  • 使用get(int index)方法遍歷鏈表
  • 排序與查找
    • 升序排序:public static sort(List list)
    • 折半法查找:int binarySearch(List list,T key,CompareTo c)
  • 洗牌與旋轉
    • 隨機排序:public static void shuffle(List list)
    • 旋轉數據:static void rotate(List list,int distance)
    • 翻轉數據:public static void reverse(List list)
  1. 堆棧
  • 建立堆棧對象:Stack
  • 壓棧操做:public E push(E item);
  • 彈棧操做:public E pop();
  • 判斷堆棧是否還有數據:public boolean empty();
  • 獲取堆棧頂端的數據,但不刪除該數據:public int search(Object data);
  1. 散列映射
  • 建立對象:HashMap<K,V>
  • 經常使用方法:
    image
  1. 樹集:TreeSet 建立對象,在使用add方法增長結點 java

  2. 樹映射:TreeMap<K,V>對象正則表達式

代碼託管

image

上週錯題總結

  1. image

    解析:image數組

  2. image

    解析:這是多態的定義,而非繼承。安全

本週學習中遇到的問題

  • 在編譯課本15章第三個例子是出現以下錯誤
    imageapp

    解決方案:在參考了注:*.java使用了未經檢查或不安全的操做 問題解決後,以及再從新閱讀課本後我發現該版本的JDK對泛型的一些要求,本身在LinkedList後面加上了 後編譯成功,但本身仍是沒有很清楚,但願老師能夠在課上講解一下。框架

相關文章
相關標籤/搜索