目錄
- 目錄
- API
- Object
- Object 類中常用方法
- String
- StringBuilder StringBuffer
- 面試題
- final
- 重寫和重載
- 封裝
- 繼承
- 01正則表達式的概念和作用
- 02正則表達式語法規則
- 03正則表達式練習和相關的String類方法
- 04正則表達式匹配練習
- 05正則表達式切割練習
- 06正則表達式替換練習
- 07正則表達式郵箱地址驗證
- 08毫秒值概念
- 09Date類的構造方法
- 10Date類的get和set方法
- 11日期格式化SimpleDateFormat
- 12字符串轉成日期對象
- 13Calendar類_1
- 14Calendar類_2
- 15Calendar類_3
- 16Calendar類_4
- 17日期練習_活了多少天
- 18日期練習_閏年計算
- 01基本數據類型對象包裝類概述
- 02Integer類parseInt方法
- 03Integer類int轉成字符串
- 04Integer類構造方法
- 05Integer類其他方法
- 06自動裝箱和自動拆箱
- 07自動裝箱和自動拆箱練習題
- 08System類方法currentTimeMillis
- 09System類方法exit
- 10System類方法gc
- 11System類方法getProperties
- 12System類方法arraycopy
- 13Math類的方法_1
- 14Math類的方法_2
- 15Arrays工具類
- 16數組複製練習
- 17BigInteger類概述和構造方法
- 19員工案例的子類的編寫
- 20BigDecimal類實現加法減法乘法
- 21BigDecimal類實現除法
- 目錄
API
api的概述: 就是java替我們寫好的一些類,他封裝了一些功能,我們僅僅只需要知道如何使用即可
Object
object的概述:
A、 object是所有的類父類
B、 object中的所有方法,子類都能使用(接口不是object的子類)
Object 類中常用方法
A、equals()
底層調用其實就是== 方法
== 方法:
基本數據類: 比較的是內容(值)
引用數據類型:比較的是內存地址值
String 的equals比較的是內容
B、String toString()
問題:爲什麼要重寫toString()方法
答:打印時默認會調用toString()方法
因爲toString()方法來源於object中,object中getClass.getName()+"@" +Integer.toHexString(hasCode() ) --->打印就是內存地址值
很多時候,我們不想看見內存地址值,想看到的是子類的特有屬性值,這時就需要重寫toString()方法
String
在String 中認爲都是對象,String str = "...";
所以str 是對象,""也是對象
String 是一個常量,其本質就是private final 修飾的字符數組
String的構造方法:
new String(byte [] bytes, int offset,int length);
offset: 數據解鎖起始位置
length:需要解鎖的位數
substring(int start ,int end ); 包含頭,不包含尾。 截取一段字符
substring(int start); 從某一位到尾
startsWith(String str) 判斷以什麼開頭
endsWith(String str) 判斷以什麼結尾
contains(String str) 判斷是否包含
indexOf(String str)
charAt(int index)
equals() 注意:要區分大小寫
equalsIgnoreCase 不區分大小寫
getBytes()
toCharArray()
StringBuilder && StringBuffer
1、 StringBuilder 是字符串緩衝對象,底層是一個沒有用final修飾的數組,並且長度默認爲16,底層是一個"可變數組"
注意:是底層自動幫我們創建新數組,數組一旦創建,是不可變的
面試題
object類有哪些方法
final
A、修飾類 : 不能被繼承
B、修飾方法 : 不能被重寫
C、修飾變量 : 基本數據類型: 值不能改變
引用數據類型: 地址值不能改變
重寫和重載
重寫:子類中出現和父類方法聲明一模一樣的方法,重寫
重載:本類中出現方法名相同,但參數列表不同,注意:與返回值類型無關
封裝
封裝好處:
A、提高了代碼的複用性
B、提高了代碼的安全性
C、隱藏了對象實現的細節,僅僅對外提供方法
繼承
A、提高了代碼的複用性
B、提高了代碼的可維護性
C、是類和類之間耦合起來了,這是多態的前提
繼承的弊端:
開發的原則:高內聚,低耦合
內聚:完成一個功能的能力
耦合:類和類之間的關係 繼承的注意事項:
1、 子類繼承父類,只能繼承父類中非private修飾的成員變量和方法
2、 簡單說:子類有,父類有,找子類,子類沒有,父類有,找父類
01正則表達式的概念和作用
* A: 正則表達式的概念和作用
* a: 正則表達式的概述
* 正則表達式也是一個字符串,用來定義匹配規則,在Pattern類中有簡單的規則定義。
可以結合字符串類的方法使用。
* 簡單記:正則表達式是具有特殊含義的字符串。
* b: 正則表達式的作用
* 比如註冊郵箱,郵箱有用戶名和密碼,一般會對其限制長度,這個限制長度的事情就是正則表達式做的
02正則表達式語法規則
* A: 正則表達式語法規則
* a: 字符
* x 代表的是字符x
* \\ 代表的是反斜線字符'\'
* \t 代表的是製表符
* \n 代表的是換行符
* \r 代表的是回車符
* b: 字符類
* [abc] a、b 或 c(簡單類)
* [^abc] 任何字符,除了 a、b 或 c(否定)
* [a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(範圍)
* [0-9] 0到9的字符都包括
* [a-zA-Z_0-9] 代表的字母或者數字或者下劃線(即單詞字符)
* c: 預定義字符類
* . 任何字符。
* \d 數字:[0-9]
* \w 單詞字符:[a-zA-Z_0-9]如"com.itheima.tests"/finish
* d: 邊界匹配器
* ^ 代表的是行的開頭
* $ 代表的是行的結尾
* \b 代表的是單詞邊界
* e: 數量詞
* X? X,一次或一次也沒有
* X* X,零次或多次
* X+ X,一次或多次
* X{n} X,恰好 n 次
* X{n,} X,至少 n 次
* X{n,m} X,至少 n 次,但是不超過 m 次
03正則表達式練習和相關的String類方法
* A: 正則表達式練習和相關的String類方法
* a: boolean matches(String 正則的規則)
* "abc".matches("[a]")
* 匹配成功返回true
* b: String[] split(String 正則的規則)
* "abc".split("a")
* 使用規則將字符串進行切割
* c: String replaceAll( String 正則規則,String 字符串)
* "abc0123".repalceAll("[\\d]","#")
* 按照正則的規則,替換字符串
04正則表達式匹配練習
* A: 正則表達式匹配練習
* a: 案例代碼
public class RegexDemo {
public static void main(String[] args) {
checkTel();
}
/*
* 檢查手機號碼是否合法
* 1開頭 可以是34578 0-9 位數固定11位
*/
public static void checkTel(){
String telNumber = "1335128005";
//String類的方法matches
boolean b = telNumber.matches("1[34857][\\d]{9}");
System.out.println(b);
}
/*
* 檢查QQ號碼是否合法
* 0不能開頭,全數字, 位數5,10位
* 123456
* \\d \\D匹配不是數字
*/
public static void checkQQ(){
String QQ = "123456";
//檢查QQ號碼和規則是否匹配,String類的方法matches
boolean b = QQ.matches("[1-9][\\d]{4,9}");
System.out.println(b);
}
}
05正則表達式切割練習
* A: 正則表達式切割練習
* a: 案例代碼
public class RegexDemo1 {
public static void main(String[] args) {
split_1();
split_2();
split_3();
}
/*
* String類方法split對字符串進行切割
* 192.168.105.27 按照 點切割字符串
*/
public static void split_3(){
String ip = "192.168.105.27";
String[] strArr = ip.split("\\.");
System.out.println("數組的長度"+strArr.length);
for(int i = 0 ; i < strArr.length ; i++){
System.out.println(strArr[i]);
}
}
/*
* String類方法split對字符串進行切割
* 18 22 40 65 按照空格切割字符串
*/
public static void split_2(){
String str = "18 22 40 65";
String[] strArr = str.split(" +");
System.out.println("數組的長度"+strArr.length);
for(int i = 0 ; i < strArr.length ; i++){
System.out.println(strArr[i]);
}
}
/*
* String類方法split對字符串進行切割
* 12-25-36-98 按照-對字符串進行切割
*/
public static void split_1(){
String str = "12-25-36-98";
//按照-對字符串進行切割,String類方法split
String[] strArr = str.split("-");
System.out.println("數組的長度"+strArr.length);
for(int i = 0 ; i < strArr.length ; i++){
System.out.println(strArr[i]);
}
}
}
06正則表達式替換練習
* A: 正則表達式替換練習
* a: 案例代碼
public class RegexDemo1 {
public static void main(String[] args) {
replaceAll_1();
}
/*
* "Hello12345World6789012"將所有數字替換掉
* String類方法replaceAll(正則規則,替換後的新字符串)
*/
public static void replaceAll_1(){
String str = "Hello12345World6789012";
str = str.replaceAll("[\\d]+", "#");
System.out.println(str);
}
}
07正則表達式郵箱地址驗證
* A: 正則表達式郵箱地址驗證
* a: 案例代碼
public class RegexDemo2 {
public static void main(String[] args) {
checkMail();
}
/*
* 檢查郵件地址是否合法
* 規則:
* [email protected]
* [email protected]
* [email protected]
* [email protected]
*
* @: 前 數字字母_ 個數不能少於1個
* @: 後 數字字母 個數不能少於1個
* .: 後面 字母
*
*/
public static void checkMail(){
String email ="[email protected]";
boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
System.out.println(b);
}
}
08毫秒值概念
* A: 毫秒值概念
* a: 時間和日期類
* java.util.Date
* b: 毫秒概念
* 1000毫秒=1秒
* c: 毫秒的0點
* System.currentTimeMillis() 返回值long類型參數
* 獲取當前日期的毫秒值 3742769374405
* 時間原點; 公元1970年1月1日,午夜0:00:00 英國格林威治 毫秒值就是0
* 時間2088年8月8日
* 時間和日期的計算,必須依賴毫秒值
09Date類的構造方法
* A: Date類的構造方法
* a: 空參構造
* public Date()
* b: 帶參構造
* public Date(long times)
==============================第二階段====================================
10Date類的get和set方法
* A:Date類的get和set方法
* public long getTime()
* 將當前的日期對象,轉爲對應的毫秒值
* public void setTime(long times);
* 根據給定的毫秒值,生成對應的日期對象
11日期格式化SimpleDateFormat
* A: 日期格式化SimpleDateFormat
* a: 對日期進行格式化(自定義)
* 對日期格式化的類 java.text.DateFormat 抽象類, 普通方法,也有抽象的方法
* 實際使用是子類 java.text.SimpleDateFormat 可以使用父類普通方法,重寫了抽象方法
* b: 對日期進行格式化的步驟
* 1: 創建SimpleDateFormat對象
* 在類構造方法中,寫入字符串的日期格式 (自己定義)
* 2: SimpleDateFormat調用方法format對日期進行格式化
* public String format(Date date) 傳遞日期對象,返回字符串
* 日期模式:
* yyyy 年份
* MM 月份
* dd 月中的天數
* HH 0-23小時
* mm 小時中的分鐘
* ss 秒
* yyyy年MM月dd日 HH點mm分鐘ss秒 漢字修改,: - 字母表示的每個字段不可以隨便寫
12字符串轉成日期對象
* A: 字符串轉成日期對象
* a: 使用步驟
* 1: 創建SimpleDateFormat的對象
* 構造方法中,指定日期模式
* 2: 子類對象,調用方法 parse 傳遞String,返回Date
* 注意: 時間和日期的模式yyyy-MM-dd, 必須和字符串中的時間日期匹配
13Calendar類_1
* A: Calendar類_1
* a: 日曆類(抽象類)
* java.util.Calendar
* b: 創建對象
* Calendar類寫了靜態方法 getInstance() 直接返回了子類的對象
* 不需要直接new子類的對象,通過靜態方法直接獲取
14Calendar類_2
* A: Calendar類_2
* a: 成員方法
* getTime() 把日曆對象,轉成Date日期對象
* get(日曆字段) 獲取指定日曆字段的值
* b: 代碼演示
Calendar c = Calendar.getInstance();
// 獲取年份
int year = c.get(Calendar.YEAR);
// 獲取月份
int month = c.get(Calendar.MONTH) + 1;
// 獲取天數
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
15Calendar類_3
*
A: Calendar類_3
* a: 成員方法
* set(int field,int value) 設置指定的時間
* b: 代碼演示
/*
* Calendar類的set方法 設置日曆 set(int field,int value) field 設置的是哪個日曆字段 value
* 設置後的具體數值
*
* set(int year,int month,int day) 傳遞3個整數的年,月,日
*/
public static void function_1() {
Calendar c = Calendar.getInstance();
// 設置,月份,設置到10月分
// c.set(Calendar.MONTH, 9);
// 設置年,月,日
c.set(2099, 4, 1);
// 獲取年份
int year = c.get(Calendar.YEAR);
// 獲取月份
int month = c.get(Calendar.MONTH) + 1;
// 獲取天數
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
}
16Calendar類_4
*
A: Calendar類_4
* a: 成員方法
* add(int field, int value) 進行整數的偏移
* int get(int field) 獲取指定字段的值
* b: 案例演示
/*
* Calendar類方法add 日曆的偏移量,
* 可以指定一個日曆中的字段,
* 進行整數的偏移 add(int field, int value)
*/
public static void function_2() {
Calendar c = Calendar.getInstance();
// 讓日曆中的天數,向後偏移280天
c.add(Calendar.DAY_OF_MONTH, -280);
// 獲取年份
int year = c.get(Calendar.YEAR);
// 獲取月份
int month = c.get(Calendar.MONTH) + 1;
// 獲取天數
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + month + "月" + day + "日");
}
17日期練習_活了多少天
* A: 日期練習_活了多少天
* a: 案例代碼
/*
* 計算活了多少天
* 生日 今天的日期
* 兩個日期變成毫秒值,減法
*/
public static void function() throws Exception {
System.out.println("請輸入出生日期 格式 YYYY-MM-dd");
//獲取出生日期,鍵盤輸入
String birthdayString = new Scanner(System.in).next();
//將字符串日期,轉成Date對象
//創建SimpleDateFormat對象,寫日期模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//調用方法parse,字符串轉成日期對象
Date birthdayDate = sdf.parse(birthdayString);
//獲取今天的日期對象
Date todayDate = new Date();
//將兩個日期轉成毫秒值,Date類的方法getTime
long birthdaySecond = birthdayDate.getTime();
long todaySecond = todayDate.getTime();
long secone = todaySecond-birthdaySecond;
if(secone < 0){
System.out.println("還沒出生呢");
}
else{
System.out.println(secone/1000/60/60/24);
}
}
18日期練習_閏年計算
* A: 日期練習_閏年計算
* a: 案例代碼
/*
* 閏年計算
* 2000 3000
* 高級的算法: 日曆設置到指定年份的3月1日,add向前偏移1天,獲取天數,29閏年
*/
public static void function_1(){
Calendar c = Calendar.getInstance();
//將日曆,設置到指定年的3月1日
c.set(2088, 2, 1);
//日曆add方法,向前偏移1天
c.add(Calendar.DAY_OF_MONTH, -1);
//get方法獲取天數
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
}
01基本數據類型對象包裝類概述
*A:基本數據類型對象包裝類概述
*a.基本類型包裝類的產生
在實際程序使用中,程序界面上用戶輸入的數據都是以字符串類型進行存儲的。而程序開發中,我們需要把字符串數據,根據需求轉換成指定的基本數據類型,如年齡需要轉換成int類型,考試成績需要轉換成double類型等
*b.八種基本類型對應的包裝類
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
02Integer類parseInt方法
*A:Integer類parseInt方法:
*a:parseInt()
int i = Integer.parseInt("12");
System.out.println(i/2);//6
*b:parseInt(String s, int radix)
/*
* Integer類靜態方法parseInt(String s, int radix)
* radix基數,進制
* "110",2 含義 前面的數字是二進制的,但是方法parseInt運行結果都是十進制
* 指定進制的字符串轉換爲十進制的整數
*/
public static void function_1(){
int i = Integer.parseInt("110", 2);
System.out.println(i);
}
03Integer類int轉成字符串
*A:Integer類int轉成字符串:
*a:使用+與字符串拼接
int i = 3;
String s = i+"";
System.out.println(s+1);//"31"
*b:toString(int ,int 進制),任意進制整數轉成任意進制的字符串 (瞭解)
String s1 = Integer.toString(5,2);
System.out.println(s1);
04Integer類構造方法
*A:Integer類構造方法
/*
* Integer類構造方法
* Integer (String s)
* 將數字格式的字符串,傳遞到Integer類的構造方法中
* 創建Integer對象,包裝的是一個字符串
* 將構造方法中的字符串,轉成基本數據類型,調用方法,非靜態的, intValue()
*/
public static void function_3(){
Integer in = new Integer("100");
int i = in.intValue();
System.out.println(--i);//99
}
05Integer類其他方法
*A:Integer類其他方法
/*
* Integer類的3個靜態方法
* 做進制的轉換
* 十進制轉成二進制 toBinarString(int)
* 十進制轉成八進制 toOctalString(int)
* 十進制轉成十六進制 toHexString(int)
* 三個方法,返回值都是以String形式出現
*/
a:十進制轉二,八,十六進制
public static void function_1(){
System.out.println(Integer.toBinaryString(99));
System.out.println(Integer.toOctalString(99));
System.out.println(Integer.toHexString(999));
}
b:獲取int的最大值和最小值
/*
* Integer類的靜態成員變量
* MAX_VALUE
* MIN_VALUE
*/
public static void function(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
06自動裝箱和自動拆箱
*A:自動裝箱與自動拆箱:
//JDK1.5新特性
//自動裝箱,拆箱的 好處: 基本類型和引用類直接運算
//自動裝箱:使用Integer.valueOf(整數值)返回一個封裝了該整數值的Integer對象
//自動拆箱:使用Integer對象.intValue()返回Integer對象中封裝的整數值
public static void function(){
//引用類型 , 引用變量一定指向對象
//自動裝箱, 基本數據類型1, 直接變成了對象
Integer in = 1; // Integer in = new Integer(1)
//in 是引用類型,不能和基本類型運算, 自動拆箱,引用類型in,轉換基本類型
//in+1 ==> in.inValue()+1 = 2
//in = 2 自動裝箱
in = in + 1;
System.out.println(in);
}
07自動裝箱和自動拆箱練習題
*A:自動裝箱與自動拆箱:
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);// false 對象地址
System.out.println(i.equals(j));// true 繼承Object重寫equals,比較的對象數據
System.out.println("===================");
Integer a = 500;//Integer integer=Integer.valueOf(500)
//integer=new Integer(500);
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("===================");
//數據在byte(-128~127)範圍內,JVM不會從新new對象
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa;
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true
=========================第二節課開始====================================
08System類方法currentTimeMillis
*A:System類方法currentTimeMillis():用於計算程序的執行時間
/*
* 獲取系統當前毫秒值
* static long currentTimeMillis()
* 對程序執行時間測試
*/
public static void function(){
long start = System.currentTimeMillis();//當前時間x-1970年1月1日零時零分零秒
for(int i = 0 ; i < 10000; i++){
System.out.println(i);
}
long end = System.currentTimeMillis();//當前時間y-1970年1月1日零時零分零秒
System.out.println(end - start);//當前時間y-當前時間x
}
09System類方法exit
*A:System類方法exit()方法
/*
* 退出虛擬機,所有程序全停止
* static void exit(0)
*/
public static void function_1(){
while(true){
System.out.println("hello");
System.exit(0);//該方法會在以後的finally代碼塊中使用(講到再說)
}
}
10System類方法gc
A:System類方法gc
public class Person {
public void finalize(){
System.out.println("垃圾收取了");
}
}
/*
* JVM在內存中,收取對象的垃圾
* 當沒有更多引用指向該對象時,會自動調用垃圾回收機制回收堆中的對象
* 同時調用回收對象所屬類的finalize方法()
* static void gc()
*/
public static void function_2(){
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
System.gc();
}
11System類方法getProperties
A:System類方法getProperties(瞭解)
/*
* 獲取當前操作系統的屬性:例如操作系統名稱,
* static Properties getProperties()
*/
public static void function_3(){
System.out.println( System.getProperties() );
}
12System類方法arraycopy
A:System類方法arraycopy:
/*
* System類方法,複製數組
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要複製的源數組
* int srcPos, 數組源的起始索引
* Object dest,複製後的目標數組
* int destPos,目標數組起始索引
* int length, 複製幾個
*/
public static void function_4(){
int[] src = {11,22,33,44,55,66};
int[] desc = {77,88,99,0};
System.arraycopy(src, 1, desc, 1, 2);//將src數組的1位置開始(包含1位置)的兩個元素,拷貝到desc的1,2位置上
for(int i = 0 ; i < desc.length ; i++){
System.out.println(desc[i]);
}
}
================================第三節課開始======================================================
13Math類的方法_1
A:Math類中的方法
/*
* static double sqrt(double d)
* 返回參數的平方根
*/
public static void function_4(){
double d = Math.sqrt(-2);
System.out.println(d);
}
/*0
* static double pow(double a, double b)
* a的b次方
*/
public static void function_3(){
double d = Math.pow(2, 3);
System.out.println(d);
}
/*
* static double floor(double d)
* 返回小於或者等於參數d的最大整數
*/
public static void function_2(){
double d = Math.floor(1.5);
System.out.println(d);
}
/*
* static double ceil(double d)
* 返回大於或者等於參數d的最小整數
*/
public static void function_1(){
double d = Math.ceil(5.1);
System.out.println(d);
}
/*
* static int abs(int i)
* 獲取參數的絕對值
*/
public static void function(){
int i = Math.abs(0);
System.out.println(i);
}
14Math類的方法_2
A:Math類的方法_2
/*
* static double round(doubl d)
* 獲取參數的四捨五入,取整數
*/
public static void function_6(){
double d = Math.round(5.4195);
System.out.println(d);
}
/*
* static double random() 返回隨機數 0.0-1.0之間
* 來源,也是Random類
*/
public static void function_5(){
for(int i = 0 ; i < 10 ;i++){
double d = Math.random();
System.out.println(d);
}
}
15Arrays工具類
A:Arrays工具類:
public class ArraysDemo {
public static void main(String[] args) {
function_2();
int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr);
System.out.println(Arrays.toString(newArray));
}
/*
* 定義方法,接收輸入,存儲的是10個人考試成績
* 將最後三個人的成績,存儲到新的數組中,返回新的數組
*/
public static int[] test(int[] arr){
//對數組排序
Arrays.sort(arr);
//將最後三個成績存儲到新的數組中
int[] result = new int[3];
//成績數組的最後三個元素,複製到新數組中
// System.arraycopy(arr, 0, result, 0, 3);
for(int i = 0 ; i < 3 ;i++){
result[i] = arr[i];
}
return result;
}
/*
* static String toString(數組)
* 將數組變成字符串
*/
public static void function_2(){
int[] arr = {5,1,4,6,8,9,0};
String s = Arrays.toString(arr);
System.out.println(s);
}
/*
* static int binarySearch(數組, 被查找的元素)
* 數組的二分搜索法
* 返回元素在數組中出現的索引
* 元素不存在, 返回的是 (-插入點-1)
*/
public static void function_1(){
int[] arr = {1,4,7,9,11,15,18};
int index = Arrays.binarySearch(arr, 10);
System.out.println(index);
}
/*
* static void sort(數組)
* 對數組升序排列
*/
public static void function(){
int[] arr = {5,1,4,6,8,9,0};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
16數組複製練習
*A:數組複製練習:
public static void main(String[] args) {
int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr);
System.out.println(Arrays.toString(newArray));
}
/*
* 定義方法,接收輸入,存儲的是10個人考試成績
* 將最後三個人的成績,存儲到新的數組中,返回新的數組
*/
public static int[] test(int[] arr){
//對數組排序
Arrays.sort(arr);
//將最後三個成績存儲到新的數組中
int[] result = new int[3];
//成績數組的最後三個元素,複製到新數組中
//System.arraycopy(arr, 0, result, 0, 3);
for(int i = 0 ; i < 3 ;i++){
result[i] = arr[i];
}
return result;
}
====================第四節課開始============================
17BigInteger類概述和構造方法
A:BigInteger類概述和構造方法
public static void main(String[] args) {
function();
}
/*
* BigInteger類的構造方法
* 傳遞字符串,要求數字格式,沒有長度限制
*/
public static void function(){
BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
System.out.println(b);
BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
System.out.println(b1);
}
###18BigInteger類四則運算
A:BigInteger類四則運算
public static void main(String[] args) {
function_1();
}
/*
* BigInteger對象的四則運算
* 調用方法計算,計算結果也只能是BigInteger對象
*/
public static void function_1(){
BigInteger b1 = new BigInteger("5665464516451051581613661405146");
BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
//計算 b1+b2對象的和,調用方法 add
BigInteger bigAdd = b1.add(b2);//965855867126930032902103163227322810292
System.out.println(bigAdd);
//計算b1-b2對象的差,調用方法subtract
BigInteger bigSub = b1.subtract(b2);
System.out.println(bigSub);
//計算b1*b2對象的乘積,調用方法multiply
BigInteger bigMul = b1.multiply(b2);
System.out.println(bigMul);
//計算b2/b1對象商,調用方法divied
BigInteger bigDiv = b2.divide(b1);
System.out.println(bigDiv);
}
19員工案例的子類的編寫
A:BigDecimal類概述
/*
* 計算結果,未知
* 原因: 計算機二進制中,表示浮點數不精確造成
* 超級大型的浮點數據,提供高精度的浮點運算, BigDecimal
System.out.println(0.09 + 0.01);//0.09999999999999999
System.out.println(1.0 - 0.32);//0.6799999999999999
System.out.println(1.015 * 100);//101.49999999999999
System.out.println(1.301 / 100);//0.013009999999999999
*/
20BigDecimal類實現加法減法乘法
A:BigDecimal類實現加法減法乘法
/*
* BigDecimal實現三則運算
* + - *
*/
public static void function(){
BigDecimal b1 = new BigDecimal("0.09");
BigDecimal b2 = new BigDecimal("0.01");
//計算b1+b2的和,調用方法add
BigDecimal bigAdd = b1.add(b2);
System.out.println(bigAdd);
BigDecimal b3 = new BigDecimal("1");
BigDecimal b4 = new BigDecimal("0.32");
//計算b3-b2的差,調用方法subtract
BigDecimal bigSub = b3.subtract(b4);
System.out.println(bigSub);
BigDecimal b5 = new BigDecimal("1.015");
BigDecimal b6 = new BigDecimal("100");
//計算b5*b6的成績,調用方法 multiply
BigDecimal bigMul = b5.multiply(b6);
System.out.println(bigMul);
}
21BigDecimal類實現除法
A:BigDecimal類實現除法
/*
* BigDecimal實現除法運算
* divide(BigDecimal divisor, int scale, int roundingMode)
* int scale : 保留幾位小數
* int roundingMode : 保留模式
* 保留模式 閱讀API文檔
* static int ROUND_UP 向上+1
* static int ROUND_DOWN 直接捨去
* static int ROUND_HALF_UP >= 0.5 向上+1
* static int ROUND_HALF_DOWN > 0.5 向上+1 ,否則直接捨去
*/
public static void function_1(){
BigDecimal b1 = new BigDecimal("1.0301");
BigDecimal b2 = new BigDecimal("100");
//計算b1/b2的商,調用方法divied
BigDecimal bigDiv = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//0.01301
System.out.println(bigDiv);
}