1. 程式人生 > >百戰程序員7-常用類

百戰程序員7-常用類

split current sta true 代碼分析 apr tac cell 類型轉換

  1、為什麽需要包裝類?包裝類的作用是?

  我們知道Java是一個面相對象的編程語言,基本類型並不具有對象的性質,為了讓基本類型也具有對象的特征,就出現了包裝類型(如我們在使用集合類型Collection時就一定要使用包裝類型而非基本類型),它相當於將基本類型“包裝起來”,使得它具有了對象的性質,並且為其添加了屬性和方法,豐富了基本類型的操作。

另外,當需要往ArrayList,HashMap中放東西時,像int,double這種基本類型是放不進去的,因為容器都是裝object的,這是就需要這些基本類型的包裝器類了。

    Java中基本數據類型與包裝類型有:

    技術分享圖片

  2、將字符串"123"轉化成基本類型數字的方式有哪些?

    Integer.parseInt("123")

  3、自動裝箱和自動拆箱指的是?舉例說明。

  裝箱就是自動將基本數據類型轉換為包裝器類型;拆箱就是自動將包裝器類型轉換為基本數據類型。

//自動裝箱
2 Integer total = 99;
3 
4 //自定拆箱
5 int totalprim = total;

  4、【上機】完成老師課堂上Integer的測試,並自己寫Double類的測試代碼

  5、為什麽String類被稱為不可變字符序列?從String類的源代碼分析,給出解釋。

  字符串是常量,String對象是不可變的,所以可以共享

  String類的底層是char類型的 數組value,這個數組用了final

  6、【上機】String類的equals方法跟Object的equals方法什麽關系? 並詳細閱讀
String類的equals方法源代碼,分析它的內部流程。

技術分享圖片

  7、String類的trim()方法是什麽作用?

    返回一個去掉字符串前後空格的字符串

  8、"hamburger".substring(4, 8) 返回的結果是?

    urge

  9、【上機】分析下面代碼的結果,並畫出內存結構圖,針對每個打印的結果給出文字

解釋。

String s = "abc";

String ss = "abc";

String s3 = "abc"+"def"; //此處編譯器做了優化!

String s4 = "abcdef";

String s5 = ss+"def";

String s2 = new String("abc");

System.out.println(s==ss);

System.out.println(s3==s4);

System.out.println(s4==s5);

System.out.println(s4.equals(s5));

System.out.println(s==ss); // true // 直接比較地址是否相同

System.out.println(s3==s4); // true

System.out.println(s4==s5); // false

// 此處根據String中equals源碼可以看出,先比較內存地址是否相同,

// 如果內存地址相同,直接返回true,

// 如果內存地址不同,進行判斷是否是String類型,

// 如果是String類型,進行向下類型轉換,轉換成String

// 然後變改成char類型數組,比較兩個數對應上的數值是否相同,

// 如果相同返回true,如果不同返回false

System.out.println(s4.equals(s5)); // true String底層結構是char類型數組

  10、【上機】練習String類的常用方法

  

  12、StringBuffer和StringBuilder的聯系是?區別是?

    三者在執行速度方面的比較:StringBuilder > StringBuffer > String

    StringBuilder:線程非安全的

    StringBuffer:線程安全的

  13、如下的代碼會造成什麽後果?運行期間會產生多少個對象? 使用StringBuilder修
改這段代碼。

String s = "";

for(int i=0;i<10000;i++){

s +=i;

}

  10001個對象,會降低效率

  StringBuilder s = new StringBuilder();

  14、計算機中的時間是如何表示的?

  技術分享圖片

  15、System.currentTimeMillis()表示什麽意思?

  返回以毫秒為單位的當前時間

  16、Date d = new Date()表示的是當前時間嗎?

   是

  17、【上機】我們使用SimpleDateFormat類來實現時間跟字符串的轉化。常用那兩
個方法?並寫出代碼,舉例說明

  

Date date = new Date();  
//格式

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

//日期轉字符串
String date2 = simpleDateFormat.format(date);

//字符串轉日期
Date date3 = simpleDateFormat.parse("2019-02-02");

    

  18、將1990年3月3日通過Calendar來表示,並得出這天是該年的第一天?將該日
期增加35天,是哪一天?使用代碼來說明。

  

        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(simpleDateFormat.parse("1990-3-3"));
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        calendar.add(Calendar.DATE, 35);
        System.out.println(calendar.get(Calendar.YEAR)+"年"+
                (calendar.get(Calendar.MONTH)+1)+"月"+
                calendar.get(Calendar.DATE)+"號");

  19、【上機】寫代碼測試Date/SimpleDateFormat/Calendar的用法。

     public static void main(String[] args){

                    // Date的用法

                    Date date1 = new Date();

                    System.out.println(date1.toString());

                    // SimpleDateFormat的用法

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

                    String daytime = sdf.format(new Date());

                    // Calendar的用法

                    GregorianCalendar calendar = new GregorianCalendar();

                    calendar.set(Calendar.YEAR,2009);

             }

  20、【上機】模仿老師的寫法,完成可視化日歷程序的開發。

              package com.test;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Scanner;

public class Test { 

    public static void main(String[] args) {

        System.out.println("請輸入日期(格式為:2017-3-3):");

        Scanner sc = new Scanner(System.in);

        String date = sc.nextLine();

        String[] daytime = date.split("-");

        int year = Integer.parseInt(daytime[0]);

        int month = Integer.parseInt(daytime[1]);

        int day = Integer.parseInt(daytime[2])

        GregorianCalendar calendar = new GregorianCalendar(year, month-1, day);

        calendar.set(Calendar.DATE, 1);

        // week:1-7 日一二三四五六

        int week = calendar.get(Calendar.DAY_OF_WEEK);

        System.out.println("日\t一\t二\t三\t四\t五\t六");

        for(int i = 0; i < week-1; i++){

            System.out.print("\t");

        }

        int maxDate = calendar.getActualMaximum(Calendar.DATE);

        for(int i = 1; i <= maxDate; i++){

            StringBuilder sb = new StringBuilder();

            if(calendar.get(Calendar.DATE) == day){

                sb.append(calendar.get(Calendar.DATE)+"*\t");

            }else{

                sb.append(calendar.get(Calendar.DATE)+"\t");

            }

            System.out.print(sb);

            if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){

                System.out.print("\n");

            }

            calendar.add(Calendar.DATE, 1);

        }

    }

}

  21、File類能代表一個目錄嗎?

    可以

  22、File類的方法mkdir跟mkdirs,有什麽區別?

    技術分享圖片

  23、使用File類模擬實現DOS的dir和dir/s命令

  24、【上機】使用遞歸算法,根據老師的樹狀結構代碼,完整展示一個目錄樹

  25、【上機】手動定義一個枚舉,表示十二個月的英文月份。

  

public enum Month{
        JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,
        SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER
    }
  1. 【上機】switch語句中的表達式結果可以是枚舉嗎? 模仿課堂代碼,寫一個例子
    說明。

    可以。

第八章 容器

百戰程序員7-常用類