1. 程式人生 > >5.2 5.3 學習內容總結

5.2 5.3 學習內容總結

byw999

一:
對象數組:存儲對象的一個數組

  1. Student[] student = new Student[5] ;
    int[] arr = {24,69,80,57,13} ;
  2. Arrays:針對數組操作的工具類 (提供了一些針對數組排序和二分搜索法)
    1>public static String toString(int[] a):可以將int類型的數組轉換成字符串 ([元素1,元素2,元素3...])
    直接用Arrays去調用
    String str = Arrays.toString(arr) ;
    System.out.println("str:"+str);
    2>public static void sort(int[] a)對指定的 int 型數組按數字升序進行排序

    Arrays.sort(arr);
    String str2 = Arrays.toString(arr) ;
    System.out.println("str2:"+str2);
    3>public static int binarySearch(int[] a,int key):二分搜索法: 在int類型的數組中查找key元素的索引
    Arrays.sort(arr);
    int index = Arrays.binarySearch(arr, 57) ;
    System.out.println("index:"+index);
    int index2 = Arrays.binarySearch(arr, 577) ;
    System.out.println("index2:"+index2);
    二:
    Calendar類:日歷類
    1.Calendar 類是一個抽象類,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉換提供了一些方法,並為操作日歷字段(例如獲得下星期的日期)提供了一些方法
    1> int year = rightNow.get(Calendar.YEAR) ;獲取年
    2>int month = rightNow.get(Calendar.MONTH) ;獲取月
    3>int date = rightNow.get(Calendar.DATE) ;獲取日
    2.抽象類的實例化
    public static Calendar getInstance() :通過一個靜態功能來創建日歷了對象

    3.Calendar中的兩個常用方法:
    1> public abstract void add(int field,int amount)根據日歷的規則,為給定的日歷字段添加或減去指定的時間量
    Calendar c = Calendar.getInstance() ;

    //年
    int year = c.get(Calendar.YEAR) ;
    
    //月
    int month = c.get(Calendar.MONTH) ;
    
    //日
    int date =  c.get(Calendar.DATE) ;
    
    System.out.println(year+"年"+(month+1)+"月"+date+"日");

    2> public final void set(int year, int month,int date)設置日歷字段 YEAR、MONTH 和 DAY_OF_MONTH 的值
    c.add(Calendar.YEAR, -3);
    //獲取年
    year = c.get(Calendar.YEAR) ;
    System.out.println(year+"年"+(month+1)+"月"+date+"日");*/

                                            //需求:5年後的10天前
                                            c.add(Calendar.YEAR, 5); 
                                            c.add(Calendar.DATE, -10);
    
                                            //獲取年
                                            year = c.get(Calendar.YEAR) ;
                                            date = c.get(Calendar.DATE) ;
                                            System.out.println(year+"年"+(month+1)+"月"+date+"日");
    
                                            System.out.println("------------------------------");
                        public final void set(int year, int month,int date)設置日歷字段 YEAR、MONTH 和 DAY_OF_MONTH 的值
                                            c.set(2018, 5,20);
                                            // 獲取年
                                            year = c.get(Calendar.YEAR);
                                            // 獲取月
                                            month = c.get(Calendar.MONTH);
                                            // 獲取日
                                            date = c.get(Calendar.DATE);
                                            System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    
        三:
        1.System 類包含一些有用的類字段和方法。它不能被實例化。 
     2.常用的方法:
        1>public static void gc()運行垃圾回收器。 
         2>public static void exit(int status)終止當前正在運行的 Java 虛擬機。參數用作狀態碼;  一般情況,需要終止
                                                    Jvm,那麽參數0
                                                public static long currentTimeMillis()返回以毫秒為單位的當前時間
    
                                    public class SystemDemo {
    
                                        public static void main(String[] args) {
    
                                            //創建一個Person類的對象
                                            Person p = new Person("高圓圓", 27) ;
                                            System.out.println(p);
    
                                            //讓p對象不指定堆內存了
                                            p = null ;
                                            System.gc(); //運行垃圾回收器,實質是執行的finalize()方法
                                        }
                                    }

    3>從指定源數組中復制一個數組,復制從指定的位置開始,到目標數組的指定位置結束
    src:原數組
    dest:目標數組
    srcPos :從原數組的哪個位置開始
    destPos:到目標數組的哪個位置結束
    length:長度
    四:
    java.util.Date:

    1. 類 Date 表示特定的瞬間,精確到毫秒
      1. 構造方法:
        1>public Date()表示分配它的時間(精確到毫秒)。
        2>public Date(long date):創建一個日期對象,指定毫秒值 (需要將long 時間毫秒值轉換成Date對象)
        3>public void setTime(long time):設置毫秒值
  3. .重點:Date的日期格式和日期的文本格式:String類型 之間進行轉換

    Date---->String(格式化)

    String-->Date(解析)

    中間的轉換:使用中一個中間類:DateFormat,並且DateFormat是一個抽象類,抽象意味著不能實例化,所以應該考慮用它的子類:
    SimpleDateFormat 是一個以與語言環境有關的方式來格式化和解析日期的具體類。它允許進行格式化(日期 -> 文本)、解析(文本 -> 日期)和規範化。

    SimpelDateFormat的構造方法:
    public SimpleDateFormat(String pattern) :構造一個SimpleDateFormat對象,根據pattern(模式:規則)

    SimpleDateFormat sdf = new SimpleDateFormat("xxx年xx月xx日") ;

    日期和時間模式

    年: yyyy
    月: MM
    日: dd
    時: hh
    分: mm
    秒: ss

    實際開發中:牽扯時間的東西,經常Date--String String--Date

public class DateDemo3 {

public static void main(String[] args) throws ParseException {

    Date---String:(格式化)
    創建一個日期對象
    Date d = new Date() ;
    創建SimpleDateFormat類對象
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
    public final String format(Date date):格式化
    String str = sdf.format(d) ;
    System.out.println("str:"+str);

    System.out.println("----------------------");

    String:日期文本格式:---->Date:日期格式
public Date parse(String source):解析
    String strDate = "2018-6-25" ;
    創建SimpleDateFormat類對象
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日") ;
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd") ;
    註意 :simpleDateFormat在解析文本格式的時候,裏面 模式(規則)一定要和文本格式的模式一直,否則就出現PareseException
    Date dd = sdf2.parse(strDate) ;
    System.out.println("dd:"+dd);//dd:Mon Jun 25 00:00:00 CST 2018
}

}
4.這是Date和Stirng文本格式進行相互轉換的工具類
這是一個將Date的日期格式轉換成日期的文本格式
@param format 模式
@param d 需要被格式化的Date格式
@return 返回的就是日期的文本格式

                        public static String dateToString(String format,Date d) {
                            /*SimpleDateFormat sdf = new SimpleDateFormat(format) ;
                            String s = sdf.format(d) ;
                            return s ;*/

                            return new SimpleDateFormat(format).format(d) ;
                        }

                            這一個將字符串文本日期格式轉換Date的日期格式的功能
                            @param format  模式
                            @param s  需要被解析的日期文本格式
                            @return 返回的就Date日期格式
                            @throws ParseException 解析如果出問題了,就會有這個異常

                        public static Date stringToDate(String format,String s) throws ParseException {

                            return new SimpleDateFormat(format).parse(s) ;

            五:
            Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。  

常用的方法:
public static int abs(int a):絕對值
public static double ceil(double a):向上取整
public static double floor(double a):向下取整
public static int max(int a,int b):求最大值
public static int min(int a,int b):求最小值
public static double pow(double a,double b):a的b次冪
public static double random()返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0
public static int round(float a):四射五入
public static double sqrt(double a):一個數的正平方根

JDK5的特性:靜態導入(導入方法的級別)

public class MathDemo {

public static void main(String[] args) {

    //public static int abs(int a):絕對值
    System.out.println(Math.abs(-10));
    System.out.println(Math.abs(10));

    //public static double ceil(double a):向上取整
    System.out.println(Math.ceil(12.34));

    //public static double floor(double a):向下取整
    System.out.println(Math.floor(12.34));

    //public static int max(int a,int b):求最大值
    System.out.println(Math.max(10, 20));
    //方法中嵌套方法

    //方法遞歸(方法本身調用方法的這種現象)
    System.out.println(Math.max(Math.max(10, 20), 20));

    //public static double pow(double a,double b):a的b次冪
    System.out.println(Math.pow(2.0, 3.0));

    //public static double random()返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0
    System.out.println(Math.random());

    //public static int round(float a):四射五入
    System.out.println(Math.round(12.56));

// public static double sqrt(double a):一個數的正平方根
System.out.println(Math.sqrt(4));

    System.out.println("---------------------------------------");

    //面試題:有兩個變量,讓他們的值進行互換 (考官想問的是:你能否指定位^的特點)
    int a = 10 ;
    int b = 20 ;

    //實際開發中:中間變量的方式進行互換

    //位^的特點:一個數據被另一個數據位^兩次,其值是它本身
    /*System.out.println(a^b^b);
    System.out.println(a^b^a);*/

    System.out.println("a:"+a+",b:"+b);

    //=號左邊: a ,b,a
    //=右邊: a^b
    a = a ^ b ; 
    b = a ^ b ;//b = a^b ^ b
    a = a ^ b ; 

    System.out.println("a:"+a+",b:"+b);

    六:
            Random:是一個可以獲取隨機數的類
                             public Random():無參構造方法
                             public Random(long seed) :指定long類型的數據進行構造隨機數類對象 
                             public int nextInt():獲取隨機數,它的範圍是在int類型範圍之 
                             public int nextInt(int n):獲取隨機數,它的範圍是在[0,n)之 
                            public class RandomDemo {

                                public static void main(String[] args) {

                                    //創建Random類對象
                                    Random r = new Random() ;

                                    for(int x = 0 ; x < 10 ; x ++) {
                                        int n = r.nextInt(5) ;
                                        System.out.println(n);
                                    }

                七:
                public String replaceAll(String regex,String replacement)
        使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。 

@author Administrator

public class RegexDemo4 {

public static void main(String[] args) {

    //定義一個字符串
    String s = "hello12345World781323244454JavaEE" ;

    //需求:要將數字字符被替換成*
    String regex = "\\d+" ;

    //定義替換的字符串
    String ss = "*" ;

    String result = s.replaceAll(regex, ss) ;
    System.out.println("result:"+result);

        八:
    關於模式和匹配器典型調用基本順序

Pattern和Matcher

public class RegexDemo5 {

public static void main(String[] args) {
    //1)將正則表達式編譯成一個模式對象

// public static Pattern compile(String regex)將給定的正則表達式編譯到模式中
Pattern p = Pattern.compile("a*b") ;
//2)通過模式對象,構造一個匹配器對象(Matcher對象)
// Matcher matcher(String input) :模式對象轉換成匹配器對象
Matcher m = p.matcher("aaaaaab") ;

     //3)匹配器對象有一個方法:machers() ; 直接對當前字符串數據進行校驗,返回boolean
     //public boolean matches()
     boolean flag = m.matches() ;
     System.out.println("flag:"+flag);

     System.out.println("-------------------------");

     //如果單純的判斷字符串是否符合正則規則,上述代碼非常麻煩,
     String regex = "a*b" ;
     String str = "aaaaab" ;

     boolean flag2 = str.matches(regex) ;
     System.out.println("flag2:"+flag2);
}

                         九:
                         public String[] split(String regex) :字符串的分割功能

按照指定的格式進行分割,分割後返回的是一個字符串數組
String s1 = "aa,bb,cc" ;

    String[] str = s1.split(",") ;
    for (int x = 0; x < str.length; x++) {
        System.out.println(str[x]);
    }

    System.out.println("----------------------------");

    String s2 = "aa.bb.cc" ;
    String[] str2 = s2.split("\\.") ;
    for(int x  = 0 ; x <str2.length; x ++) {
        System.out.println(str2[x]);
    }

    System.out.println("----------------------------");

    String s3 = "aa               bb                                      cc" ;
    String[] str3 = s3.split(" +") ;
    for(int x = 0 ; x <str3.length ; x ++) {
        System.out.println(str3[x]);
    }
    System.out.println("----------------------------");

    //硬盤上的路徑的形式
    String s4 = "E:\\JavaSE\\Code\\day11" ;
    String[] str4 = s4.split("\\\\");
    for(int x = 0 ; x <str4.length ; x ++) {
        System.out.println(str4[x]);
    }

    十:

                                                字符

                                            x           x字符
                                            \\          反斜線字符
                                            \t          制表符 
                                            \n          換行符
                                            \r          回車符 

                                        字符類:
                                            [abc]       a、b 或 c(簡單類)
                                            [^abc]      任何字符,除了 a、b 或 c(否定)
                                            [a-zA-Z]    a 到 z 或 A 到 Z,兩頭的字母包括在內(範圍) 

                                        預定義字符類:
                                            . 任何字符      如果本身就是.     \.      qq.com  寫正則表達式(\\.) 
                                            \d              數字:[0-9]                        寫正則表達式 :\\d
                                            \w              單詞字符:[a-zA-Z_0-9]:字母大小寫,數字字符                        \\w

                                        邊界匹配器:
                                            ^                行的開頭 
                                            $                行的結尾 
                                            \b              單詞邊界 尾 (helloword?haha:world)

                                        Greedy 數量詞(重點)
                                            X?              X,一次或一次也沒有 
                                            X*              X,零次或多次 
                                            X+              X,一次或多次 
                                            X{n}            X字符恰好出現n次
                                            X{n,}           X字符至少出現n次
                                            X{n,m}          X字符至少出現n次,但不超過m次

十一:

對象數組:可以存儲對象的數組

需求:我有5個學生,5個學生有自己的姓名,年齡,遍歷當前學生數組,獲取到每一個學生的信息

1)自定義類:Student      name,age
2)在測試類中:創建一個數組,可以存儲Stduent類型的
3)根據的提供長度,分別創建5個具體的學生對象

public class ObjectArrayDemo {

public static void main(String[] args) {
    //創建一個數組,可以存儲Student類型 (對象數組)
    //創建數組的格式:int[] arr= new int[長度] ;
    Student[] students = new Student[5] ;

    //需要創建5個學生對象
    Student s1 = new  Student("高圓圓", 27) ;
    Student s2 = new  Student("王力宏", 30) ;
    Student s3 = new  Student("唐嫣", 29) ;
    Student s4 = new  Student("克勞澤",35) ;
    Student s5 = new  Student("拉姆", 36) ;

    //賦值
    students[0] = s1 ;
    students[1] = s2 ;
    students[2] = s3 ;
    students[3] = s4 ;
    students[4] = s5 ;

    //遍歷學生數組
    for(int x = 0 ; x < students.length ; x ++) {

// System.out.println(students[x]);

        Student s = students[x] ;

        System.out.println(s.getName()+"----"+s.getAge());
    }

    十二:
    集合的由來?
學生的面向對象語言,面向對象語言對事物的描述是通過對象體現的,那麽需求需要來存儲多個對象.

要存儲多個對象,不能使用基本類型的變量,需要使用容器類型的變量? 學習過哪些容器變量? 數組 ,字符串緩沖區(StringBuffer)
對於字符串緩沖區來說,在內存中始終存儲的是字符串,不能滿足要求;數組呢,數組的長度是固定的,不符合長度編號的要求,所有Java提供了一個
Collection 集合;

面試題:????
數組和集合的區別?
1)長度區別:
數組長度固定
集合長度可變
2)內容的區別
數組可以存儲同一種類型的元素
集合可以存儲多種類型的元素
3)存儲類型的區別
數組:可以存儲基本類型,也可以存儲引用類型 String[] str = {"hello","world","java",100} ; 錯誤的
集合:只能存儲引用類型

集合:Collection:子接口有兩個,兩個子接口分別對應多個子實現類,多個集合數據結構不同,但是他們有共性內容,將共性內容
抽取出來,就可以集合繼承體系圖!

數據結構:數據的存儲方式

Collection:
Collection 層次結構 中的根接口。Collection 表示一組對象,這些對象也稱為 collection 的元素。一些 collection 允許有重復的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無序的。
JDK 不提供此接口的任何直接 實現:它提供更具體的子接口,更具體的實現類
基本功能:
添加功能:
boolean add(Object e)
刪除功能:
void clear() :刪除集合中所有元素(暴力刪除)
boolean remove(Object o):刪除集合中的指定元素
判斷功能:
boolean contains(Object o):集合中是否包含指定的元素
獲取功能:
int size() :獲取集合中的元素數

boolean retainAll(Collection c) :交集功能:   A集合對B集合取交集元素  :思考
                                 boolean 表達的是什麽意思,交集的元素是去A集合還是去B集合中
boolean removeAll(Collection c):刪除一個集合中所有元素: 思考: 刪除一個元素算是刪除還是刪除所有算是刪除?

轉換功能:
Object[] toArray() :將集合轉換成數組

public class CollectionDemo {

public static void main(String[] args) {

    //創建集合對象

// Collection c = new Collecton() ; //不能實例化
Collection c = new ArrayList() ;
System.out.println(c);

    //boolean add(Object e) :添加元素
    /*boolean flag = c.add("hello") ;

      add()方法的源碼
     public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;   //只要添加就返回true
}

    c.add("hello") ;
    c.add("world") ;
    c.add("java") ;
    System.out.println("c:"+c) ;

    //刪除功能:

// c.clear();
System.out.println("remove():"+c.remove("java"));
System.out.println("c:"+c);

    //boolean contains(Object o):集合中是否包含指定的元素
    System.out.println("contains():"+c.contains("android"));
    System.out.println("contains():"+c.contains("hello"));

    System.out.println(c.size());
    //boolean isEmpty() :判斷集合是否為空
    System.out.println(c.isEmpty());
}

________________________________________________

5.3

一:
Colleciton的集合的高級功能:
boolean addAll(Collection c) :添加一個集合中的所有元素
boolean removeAll(Collection c):刪除的高級功能(思考:刪除一個算是刪除還是刪除所有算是刪除?)
boolean containsAll(Collection c):包含所有元素算是包含,還是包含一個算是包含

交集功能:
boolean retainAll(Collection c):A集合對B集合取交集,交集的元素到底是去A集合還是去B集合中,返回值boolean
表達什麽意思?

public class CollectionDemo {

public static void main(String[] args) {

    //創建Collection集合1
    Collection c1 = new ArrayList() ;

    //添加元素
    c1.add("abc1") ;
    c1.add("abc2") ;
    c1.add("abc3") ;
    c1.add("abc4") ;

    //創建第二個集合
    Collection c2 = new ArrayList() ;
    c2.add("abc1") ;
    c2.add("abc2") ;
    c2.add("abc3") ;
    c2.add("abc4") ;

// c2.add("abc5") ;
// c2.add("abc6") ;
// c2.add("abc7") ;

    System.out.println("c1:"+c1);
    System.out.println("c2:"+c2);
    System.out.println("--------------------------------");
    //boolean addAll(Collection c)  :添加一個集合中的所有元素
    /*System.out.println("addAll():"+c1.addAll(c2));*/
    //  boolean removeAll(Collection c):刪除的高級功能(思考:刪除一個算是刪除還是刪除所有算是刪除?)
    //結論:刪除一個算是刪除...

// System.out.println("removeAll():"+c1.removeAll(c2));

    //boolean containsAll(Collection c):包含所有元素算是包含,還是包含一個算是包含
    //結論:包含所有算是包含

// System.out.println("containsAll():"+c1.containsAll(c2));

    //  boolean retainAll(Collection c):A集合對B集合取交集,交集的元素到底是去A集合還是去B集合中,
        //返回值boolean表達什麽意思?
    /**
     * 結論:A集合對B集合取交集,交集的元素要去A集合中,boolean返回值表達的A集合的元素是否發生變化,如果發生變化,則返回true,否則,返回false
     */
    System.out.println("retainAll():"+c1.retainAll(c2));

    System.out.println("c1:"+c1);
    System.out.println("c2:"+c2);
}
二:

轉換功能:Object[] toArray() :將集合轉換成數組

length():屬於String類型的特有功能,可以獲取字符串長度

        String str = (String) objs[x] ;  //相當於:向下轉型
        System.out.println(str+"----"+str.length());

三:
Iterator iterator() :集合的叠代器方法(獲取集合的叠代器)

集合的專有遍歷方式:叠代器遍歷

    Iterator :接口中有以下的方法:
        boolean hasNext() :如果有元素可以叠代,那麽返回true,否則返回false
        Object next()返回叠代的下一個元素。 

存儲String類型的元素
String str = (String) obj ;
String str = (String)(it.next()) ;
System.out.println(str+"---"+str.length());
!!!!! 註意:
1)it.next(),只使用一次即可,使用多次,會出現問題(it.next(),每次使用的時候都是返回一個對象)
2)遍歷的時候使用的while循環,可不可以使用for循環呢?

四:
interface extends Collection{
就可以使用Collection集合的功能
}

Collection有兩個子接口:List Set

叠代器:Iterator iterator() ;

存儲字符串數據,並遍歷

public class ListDemo {

public static void main(String[] args) {
    //創建List集合對象
    List list = new ArrayList() ;

    //存儲元素
    list.add("hello") ;
    list.add("world") ;
    list.add("java") ;

    //獲取叠代器
    Iterator it = list.iterator() ;
    //遍歷元素
    while(it.hasNext()) {
        String s = (String)it.next() ;
        System.out.println(s+"---"+s.length());
    }

    2.List集合的特點:
有序的 (存儲和取出一致),可以允許重復元素

Set集合的特點:
無序性(不能保證叠代的順序,基於hashMap),並且元素不能重復

public class ListDemo2 {

public static void main(String[] args) {

    //創建集合對象
    List list = new ArrayList() ;

    //存儲重復元素
    list.add("hello") ;
    list.add("world") ;
    list.add("hello") ;
    list.add("java") ;
    list.add("javaee") ;
    list.add("javaee") ;
    list.add("android") ;
    list.add("android") ;

    //遍歷
    Iterator it = list.iterator() ;
    while(it.hasNext()) {
        String s = (String)it.next() ;
        System.out.println(s);

        3.List集合的特有功能:

添加功能
void add(int index,Object element):在指定位置處添加指定元素
獲取功能
Object get(int index)返回列表中指定位置的元素。
System.out.println("get():"+list.get(2));
list.add(1, "javaee");
ListIterator listIterator():列表叠代器
刪除功能:
Object remove(int index):刪除指定位置處的元素
System.out.println("remove():"+list.remove(3));
修改功能
Object set(int index, Object element):用指定element元素替換掉指定位置處的元素
System.out.println("set():"+list.set(2, "android"));

ListIterator listIterator():列表叠代器 (List集合的專有叠代遍歷:列表叠代器)
ListIterator接口中:
boolean hasNext() :判斷是否有下一個可以叠代的元素(正向遍歷)
Object next():獲取下一個元素
boolean hasPrevious():判斷是否有上一個可以叠代 元素(逆向遍歷)
Object previous():返回上一個元素

逆向叠代(遍歷),單獨使用沒意義,前提,要先正向遍歷

五:
List集合類的子實現類的特點

List集合有三個子實現類:

        ArrayList
                底層數據結構式數組結構,查詢塊,增刪慢
                從內存角度考慮:線程不安全的,不同步的,執行效率高

                多線程:synchronized :同步的意思     解決線程安全問題
                        sychronized(鎖對象){   同步代碼 
                                共享數據;   
                        }

                解決線程安全問題,通過同步可以解決,但是效率低了...

        LinkedList
                :底層數據結構式鏈表結構,查詢慢,增刪塊
                從內存角度考慮:線程不安全,不同步,執行效率高

        Vector:
                這是一個線程安全的類,
                底層數據結構是數組:查詢快,增刪慢
                線程安全的,同步,執行效率低!

5.2 5.3 學習內容總結