文件java語(yǔ)言程序設(shè)計(jì)_第1頁(yè)
文件java語(yǔ)言程序設(shè)計(jì)_第2頁(yè)
文件java語(yǔ)言程序設(shè)計(jì)_第3頁(yè)
文件java語(yǔ)言程序設(shè)計(jì)_第4頁(yè)
文件java語(yǔ)言程序設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩84頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Java語(yǔ)言程序設(shè)計(jì)Predefined

ClassHy

Mao2023年9月7日華東師范大學(xué)軟件學(xué)院Outline2023/9/72Introduction

to

Java

API鍵盤(pán)輸入和格式化輸出Predefined

ClassMathString

和String

BufferStringTokenizerDateGegorianCalendarWrapper

ClassIntroduction

to

Java

API2023/9/73API(Application Interface)應(yīng)用程序接口。Sun公司提供的JDK中包含豐富的Java類(lèi)庫(kù),在 項(xiàng)目開(kāi)發(fā)中如果能夠適當(dāng)?shù)倪\(yùn)用這些API來(lái)完成 程序設(shè)計(jì),可以使得我們的項(xiàng)目開(kāi)發(fā)達(dá)到事半功 倍的效果。怎么樣用Java的API?用Java中預(yù)先定義的Class的方法和用我們的自定義類(lèi)e.g.

Book的方法是一樣的。E.g.import

java.lang.System;當(dāng)我們用Java預(yù)定義的類(lèi)時(shí),關(guān)心的是類(lèi)能夠?qū)ν馓峁┦裁垂δ?,而不是這些功能怎么實(shí)現(xiàn)的。Introduction

to

Java

API2023/9/74從哪里可以全面的知道Java類(lèi)庫(kù)中各個(gè)類(lèi)的功能 介紹呢?Sun公司提供了JavaDocument文檔,該文檔詳細(xì)介紹了JDK的結(jié)構(gòu)特點(diǎn)和Java類(lèi)庫(kù)的功能。你可以從Sun公司的網(wǎng)站下載該幫助文檔,一般來(lái)說(shuō)

Java的幫助文檔都是以HTML文件的形式來(lái)發(fā)布,也有人把HTML文件整理成*.chm的幫助文檔格式,這樣更加方便我們查找。[幫助文檔]jdk150.chmIntroduction

to

Java

APISun公司將類(lèi)庫(kù)中的包按照功能來(lái)劃分,下表介紹了各個(gè)包的功能:Java.appletClasses

for

implementing

applets.Java.awtClasses

for

graphics,

text,

windows,

and

GUIs.Java.awt.eventClasses

for

GUI

button,

mouse

actions,

etc.Java.ioClasses

for

input

and

output.Java.langClasses

for

the

core

language.JClasses

for

networking.Java.util2023/9/7Classes

for

useful

data

types.52023/9/76鍵盤(pán)輸入和格式化輸出在Lecture02中我們介紹了用java.util.Scanner類(lèi)來(lái) 實(shí)現(xiàn)鍵盤(pán)輸入的功能。在Java中也可以用對(duì)話(huà)框的形式來(lái)接收用戶(hù)的輸 入。該類(lèi)為:javax.swing.JOptionPane從鍵盤(pán)讀入數(shù)據(jù)的方法為:static

StringshowInputDialog

(Object

message)Shows

a

question-message

dialog

requesting

inputfrom

the

user.Note:該方法為靜態(tài)方法,可以直接用類(lèi)名來(lái)調(diào)用。[參考]Book

chapter3

Input

and

Output2023/9/7

7鍵盤(pán)輸入和格式化輸出import

javax.swing.*;public

class

InputTest2

{public

static

void

main(String[]

args)

{String

name

=JOptionPane.showInputDialog("What

is

your

name?");String

input

=JOptionPane.showInputDialog("How

old

are

you?");int

age=Integer.parseInt(input);//類(lèi)型轉(zhuǎn)換System.out.println("Hello,

"

+

name

+

".

Next

year,

you'll

be

"

+(age

+

1));System.exit(0);

}

}Note:(1)showInputDialog讀取的數(shù)據(jù)都是String類(lèi)型,所以在程序中需要根據(jù)需要做類(lèi)型轉(zhuǎn)換。(2)JOptionPane.showInputDialog調(diào)用時(shí)系統(tǒng)會(huì)創(chuàng)建新的線(xiàn)程(thread),所以在每一次使用時(shí)都需要調(diào)用System.exit(0);來(lái)關(guān)閉所有的所有的線(xiàn)程。鍵盤(pán)輸入和格式化輸出當(dāng)我們向用戶(hù)反饋程序處理的結(jié)果時(shí),往往對(duì)輸 出的格式有要求,這也是程序友好性的體現(xiàn)。E.g.我們需要的輸出是:在JDK1.5中對(duì)輸出格式作了改進(jìn),目前Java支持 兩種格式化輸出的方法。C語(yǔ)言中的格式化輸出方式。(JDK1.5新支持的特點(diǎn))java.text.*包中XxxFormat類(lèi)完成格式化輸出。3,333.333$3,333.33333,33%3333.3333333333333333333.333333333333333333.33333333333333332023/9/78鍵盤(pán)輸入和格式化輸出(1)C語(yǔ)言中的格式化輸出方式的示例。我們不再一一說(shuō)明格式的用法,只是用一些實(shí)例來(lái)說(shuō)明用法,如有格式化輸出的需求時(shí),可以參考書(shū)本,

chapter3

表3-5,3-6,3-7。E.g.double

x

=

10000.0

/

3.0;System.out.printf("%8.2f",

x);注意:此時(shí)需要用的是printf方法,%是格式化輸出的提示符。%8.2f表示:輸出浮點(diǎn)類(lèi)型的數(shù)據(jù),包含兩位小數(shù)位,整個(gè)數(shù) 據(jù)為8位。輸出為:

3333.33

//

表示3前面的空白字符更多的輸出方式見(jiàn)表3-52023/9/79鍵盤(pán)輸入和格式化輸出2023/9/710E.g.(1)

System.out.printf("%,.2f",

10000.0

/

3.0);%,.2f表示:按照浮點(diǎn)數(shù)格式輸出,整數(shù)位用逗號(hào)分割,小數(shù)位為2位。輸出:3,333.33E.g.(2)

System.out.printf("%tc",

new

Date());%tc表示:t是日期輸出的提示,c表示輸出完整的日期輸出:星期五七月1317:31:38

CST

2007E.g.(3)

System.out.printf("%1$s

%2$tB

%2$te,

%2$tY",

"Due

date:",new

Date());%1$s表示:第一個(gè)參數(shù)即“Due

date:”按照字符串來(lái)輸出。%2$tB表示:第二個(gè)參數(shù)即“new

Date()”輸出英文的月。%2$te表示:第二個(gè)參數(shù)即“new

Date()”輸出一位數(shù)字日。%2$tY表示:第二個(gè)參數(shù)即“new

Date()”輸出四位數(shù)字年。輸出為:Due

date:七月13,

2007鍵盤(pán)輸入和格式化輸出2023/9/711(2)java.text.*包中XxxFormat類(lèi)完成格式化輸出。在該包 中包含以下幾個(gè)類(lèi):DateFormatSimpleDateFormat//以上兩個(gè)類(lèi)負(fù)責(zé)日期的格式化輸出NumberFormatDecimalFormat//以上兩個(gè)類(lèi)負(fù)責(zé)所有數(shù)字的格式化輸出。一般步驟創(chuàng)建格式化輸出類(lèi)的實(shí)例。用該實(shí)例處理待輸出的數(shù)據(jù),得到希望的輸出結(jié)果。[參考]

JDK幫助文檔。鍵盤(pán)輸入和格式化輸出NumberFormatN=NumberFormat.getInstance();System.out.println(1000/3.0

+""+10000);System.out.println(N.format(1000/3.0)

+""+N.format(10000));輸出為:333.3333333333333

10000333.333

10,000NumberFormatC=NumberFormat.getCurrencyInstance();System.out.println(C.format(1000));C

=

NumberFormat.getCurrencyInstance(Locale.US);System.out.println(C.format(1000));輸出為:¥1,000.00$1,000.00說(shuō)明:Locale類(lèi)位于java.util包用來(lái)說(shuō)明區(qū)域,US是Locale的靜態(tài)常量2023/9/712Predefined

Class-Math2023/9/713java.lang.Math類(lèi)提供了科學(xué)計(jì)算的功能E.g.Math類(lèi)中提供了取整的方法,如round,

floor,ceilingMath類(lèi)中提供了指數(shù)和對(duì)數(shù)運(yùn)算的方法,如log,powMath類(lèi)中提供了三角運(yùn)算的方法,如sin,cosMath類(lèi)中提供了取隨機(jī)數(shù)的方法,如random你可以用JDK1.5

doc來(lái)查看Math類(lèi)中方法和屬性的定義:在Math類(lèi)中屬性較常用的屬性常量是public

final

static

double

Math.PI//數(shù)學(xué)中PI的值public

final

static

double

Math.E//科學(xué)計(jì)數(shù)法中e的值Math類(lèi)中的方法都是靜態(tài)方法所以通過(guò)類(lèi)名直接訪(fǎng)問(wèn)。2023/9/714Predefined

Class-Math取絕對(duì)值的方法:public

static

int

abs(int

a)public

static

long

abs(long

a)public

static

float

abs(float

a)public

static

double

abs(double

a)long

ll=

Math.abs(2010L);System.out.println(ll);double

dd

=

Math.abs(-Math.PI);System.out.println(dd);20103.141592653589793Predefined

Class-Math2023/9/715取整的方法:public

static

double

ceil(double

d)//返回比d大,最接近于d的整數(shù)(返回值用double類(lèi)型來(lái)存儲(chǔ)),即為上取整public

static

double

floor(double

d)//返回比d小,最接近于d的整數(shù)(返回值用double類(lèi)型來(lái)存儲(chǔ)),即為下取整。public

static

int

round(float

f)public

static

long

round(double

d)//用四舍五入的方法取整。Math.round(-10.5);

//

result

is

–10Predefined

Class-Math2023/9/716e.g.Math.ceil(9.0)

//

result

is

9.0Math.ceil(8.8)

//

rises

to

9.0Math.ceil(8.02)

//

still

rises

to

9.0Math.ceil(-9.0)

//

result

is

–9.0Math.ceil(-9.4)

//

rises

to

–9.0,Math.ceil(-9.8)

//

still

rises

to

–9.0注意:-9.4經(jīng)過(guò)ceil運(yùn)算之后返回-9.0,因?yàn)?9.0>-9.4Math.floor(9.0)

//

result

is

9.0Math.floor(9.4)

//

drops

to

9.0Math.floor(9.8)

//

still

drops

to

9.0Math.floor(-9.0)

//

result

is

–9.0Math.floor(-8.8)

//

drops

to

–9.0Math.floor(-8.1)

//

still

drops

to

–9.0注意:-8.1經(jīng)過(guò)floor運(yùn)算之后返回-9.0,因?yàn)?9.0<-8.1Predefined

Class-Mathdouble

upPI=Math.ceil(Math.PI);System.out.println(upPI);double

downPI=Math.floor(Math.PI);System.out.println(downPI);long

roundPI=Math.round(Math.PI);System.out.println(roundPI);4.03.032023/9/717Predefined

Class-Math2023/9/718返回兩個(gè)數(shù)的最大值:public

static

int

max(int

a,

int

b)public

static

long

max(long

a,

long

b)public

static

float

max(float

a,

float

b)public

static

double

max(double

a,

double

b)e.g.

x

=Math.max(1024,

-5000);

//

outputis

1024.返回兩個(gè)數(shù)的最小值:public

static

int

min(int

a,

int

b)public

static

long

min(long

a,

long

b)public

static

float

min(float

a,

float

b)public

static

double

min(double

a,

double

b)e.g.

x

=Math.min(1024,

-5000);

//

outputis

-5000.Predefined

Class-Math2023/9/719指數(shù)運(yùn)算:求a的b次冪:public

static

doublepow(doublea,

doubleb)求Math.E的d次冪:public

static

double

exp(double

d)對(duì)數(shù)運(yùn)算:求以Math.E為底的d的對(duì)數(shù):public

static

double

log(double

d)開(kāi)方運(yùn)算:求d開(kāi)平方后的結(jié)果:public

static

double

sqr(double

d)Predefined

Class-Mathdouble

r

=

Math.pow(2.0,4.0);System.out.println(r);double

v

=

Math.exp(2.0);System.out.println(v);double

l

=

Math.log(Math.E);System.out.println(l);double

c

=

Math.sqrt(3.0*3.0

+

4.0*4.0);System.out.println(c);16.07.389056098930650.99999999999999815.02023/9/720Predefined

Class-Math三角函數(shù)的方法:public

static

double

sin(double

d)public

static

double

cos(double

d)public

static

double

tan(double

d)注意:d為弧度單位,如1/4PI,PI,2PI等double

r1

=

Math.sin(Math.PI/2.0);System.out.println(r1);double

r2

=

Math.cos(Math.PI);System.out.println(r2);double

r3

=

Math.tan(Math.PI/4.0);System.out.println(r3);1.0-1.00.99999999999999992023/9/721Predefined

Class-String

&

StringBuffer2023/9/722字符串可以用String和StringBuffer類(lèi)來(lái)描述。String類(lèi)位于java.lang.*中StringBuffer類(lèi)位于java.lang.*中在Java中所有的字符串都是用對(duì)象的方式來(lái)存儲(chǔ)。C++中的字符串可以看成字符數(shù)組,支持用數(shù)組名+下標(biāo)的方 式訪(fǎng)問(wèn)字符串中的字符,這種語(yǔ)法在java中部支持。e.g.

String

name

=

“Peter”;System.out.println(name[0]);//errorString和StringBuffers類(lèi)的區(qū)別在于:String對(duì)象存儲(chǔ)的字符串?dāng)?shù)據(jù)一旦創(chuàng)建之后是只讀的,其設(shè)定的內(nèi)容不能修改。StringBuffer對(duì)象存儲(chǔ)的字符串是可以修改的,其設(shè)定的內(nèi)容可以根據(jù)需要改變。String對(duì)象的創(chuàng)建2023/9/723字符串常量的表示:雙引號(hào)括住的一串字符表示一個(gè)字符串常量字符串常量對(duì)應(yīng)一個(gè)String類(lèi)型的對(duì)象實(shí)例e.g.

”Hello

world!”;“Good

Morning

”字符串對(duì)象的創(chuàng)建方式(1)

直接將字符串賦值給一個(gè)字符串類(lèi)型的對(duì)象。e.g.在java中String類(lèi)比較特殊,只有String類(lèi)的對(duì)象支持用字符串 常量的來(lái)完成初始化。String對(duì)象的創(chuàng)建String

name

=“ECNU”;String

s7

=

”;String

s8

=

“”;String

s9;“ECNU”name“

”s7“”s8S9

null2023/9/724String對(duì)象的創(chuàng)建2023/9/725(2)通過(guò)new運(yùn)算符來(lái)創(chuàng)建一個(gè)對(duì)象此時(shí)系統(tǒng)自動(dòng)調(diào)用構(gòu)造函數(shù)來(lái)創(chuàng)建一個(gè)對(duì)象。String類(lèi)中主要的構(gòu)造函數(shù)說(shuō)明:依據(jù)現(xiàn)有的字符串生成對(duì)象String(String

original)

;e.g.: String

str

=

new

String(“Hello!”);等價(jià)于:String str

=

“Hello!”;String(StringBuffer

buffer);依據(jù)默認(rèn)的構(gòu)造方法生產(chǎn)新的對(duì)象String();e.g.String

str=new

String();//產(chǎn)生一個(gè)空串等價(jià)于:String str

=“”;String對(duì)象的創(chuàng)建2023/9/726兩中創(chuàng)建String對(duì)象方式的區(qū)別從單條的String對(duì)象創(chuàng)建的語(yǔ)句來(lái)說(shuō),兩者方式實(shí)現(xiàn)的結(jié)果沒(méi)有區(qū)別。但是需要注意:直接用字符串常量賦值的方式中,String類(lèi)型的常量在系統(tǒng)中 作為一個(gè)匿名的對(duì)象存儲(chǔ),如果有兩個(gè)匿名對(duì)象的內(nèi)容相同, 他們會(huì)共享系統(tǒng)的空間。通過(guò)new運(yùn)算符來(lái)創(chuàng)建對(duì)象時(shí),系統(tǒng)會(huì)調(diào)用構(gòu)造方法,為當(dāng)前 的對(duì)象分配新的空間,即使前后兩次對(duì)象的內(nèi)容相同他們也會(huì) 擁有不同的對(duì)象空間。請(qǐng)看以下的實(shí)例:String對(duì)象的創(chuàng)建-ExampleString

str1

=

"you

and

me!";String

str2

="you

and

me!";You

and

mestr1str2String

str3

=

new

String("hello");String

str4

=

new

String("hello");str3str4hellohello2023/9/727String對(duì)象的創(chuàng)建-ExampleString

s1

=new

String();s1="I

like

grape

juice";System.out.println(s1);String

s2=

new

String("I

like

grape

juice");System.out.println(s2);String

s3

="bravo!";System.out.println(s3);2023/9/728Output:I

like

apple

juiceI

like

grape

juicebravo!注意:s1和s2內(nèi)容相同,但是在內(nèi)存中的存儲(chǔ)的空間不同String對(duì)象的操作-連接連接:java允許利用+號(hào)把兩個(gè)子串連結(jié)起來(lái)?!?”此時(shí)表示連接運(yùn)算,而不是加法運(yùn)算。e.g.:String

expletive

=“Explective

”;String

PG13

=

“deleted”;Stringmessage

=explective+

PG13; “Expletive

deleted”

+號(hào)將把兩個(gè)字符串按照運(yùn)算的順序連接起來(lái)。連接一個(gè)字串和一個(gè)非字符串值時(shí),后者將會(huì)轉(zhuǎn)換成字串。String

rating

=

“PG”

+13

;

“PG13”請(qǐng)看以下的實(shí)例:2023/9/729String對(duì)象的操作-連接String

s

=“hello”

+

+

“Miss

Li”;hello

Miss

LiString

s1

=

“Hi”;String

s2=“you!”;String

s3

=

s1

+

+

s2;Hi

you!System.out.println(“The

resulted

odder

numbers

are“

+

9

+

3);The

resulted

odder

numbers

are

932023/9/730String對(duì)象的操作-連接2023/9/731String類(lèi)中的concat方法和”+”具有相同的功能, 可以連接連個(gè)字符串。public

String

concat(String

str)//將當(dāng)前對(duì)象與str對(duì)象連接,并返回一個(gè)新的對(duì)象。String

motto

=

"Program

once";motto

=

motto

+

",

execute

everywhere.";motto=motto.concat("

Don't

bet

on

it!");System.out.println(motto);Output:Program

once,

execute

every

where.

Don’t

bet

on

it!String對(duì)象的操作-子串2023/9/732String類(lèi)中定義的substring方法可以從字符串對(duì) 象中提取出一個(gè)子串。String

substring(int

beginIndex);

獲得該字符串中從beginIndex開(kāi)始到字符串結(jié)束的子串String

substring(int

beginIndex,

int

endIndex);獲得該字符串中從beginIndex開(kāi)始的,到endIndex-1的子串。Eg:String

greeting =“hello”;String

s=greeting.substring(0,5);//字符串的第一個(gè)字符的位置為0等價(jià)于:String

s1=greeting.substring(0);注意:字符串的序號(hào)是從0開(kāi)始;如果程序中的序號(hào)超過(guò)了字符串的范圍,運(yùn)行時(shí)會(huì)出現(xiàn)異常。String對(duì)象的操作-子串2023/9/733String類(lèi)中提供了trim方法用來(lái)去掉原字符串中的 前后空白字符。

publicStringtrim();//trim去掉的空白字符包括了回車(chē)換行符、空白符、tab符號(hào)等。該方法在我們做實(shí)際的程序時(shí)非常有用,當(dāng)我們接收到用戶(hù)的輸入時(shí),一般需要先對(duì)用戶(hù)輸入的字符串進(jìn)行trim處理,然后再做比較等操作。String對(duì)象的操作-子串Example2023/9/734JavaNation------------JavaNation------------NationioString

uto=("\t\n

Java

Nation

\n\t");System.out.println(uto);System.out.println("------------");uto=uto.trim();System.out.println(uto);System.out.println("------------");uto=uto.substring(5);System.out.println(uto);String

radioactive=uto.substring(3,5);System.out.println(radioactive);2023/9/735String對(duì)象的操作-字符串訪(fǎng)問(wèn)字符串訪(fǎng)問(wèn)的主要內(nèi)容字符串的長(zhǎng)度、查找是否存在某個(gè)子串、獲取某個(gè)指定位置的字符……String對(duì)象的訪(fǎng)問(wèn)方法int

length();

獲取字符串所包含的字符數(shù)即字符串的長(zhǎng)度char

charAt(int

index);

獲取字符串指定位置的字符String

s=

“hello

world”charAt(0)charAt(str.length()-1)char

‘h’char

‘d’char

c=s.charAt(0);//c的值為‘h’String對(duì)象的操作-字符串訪(fǎng)問(wèn)String對(duì)象的訪(fǎng)問(wèn)方法(續(xù))查找特定子串(indexOf,

lastIndexOf)intindexOf(String

str);返回子串str在該字符串中首次出現(xiàn)位 置,若未找到則返回-1int

indexOf(String

str,int

fromIndex);返回子串str在該字符串 中索引fromIndex之后首次出現(xiàn)位置intindexOf(intch);返回字符ch在該字符串中首次出現(xiàn)位置, 若未找到則返回-1.intindexOf(int

ch,intfromIndex);返回字符ch,在該字符串 中索引fromIndex之后首次出現(xiàn)位置String=“hello

world”indexOflastIndexOf2023/9/736String對(duì)象的操作-字符串訪(fǎng)問(wèn)2023/9/737逆向查找特定子串int

lastIndexOf(String

str);

返回子串str在該字符串中最后一次出現(xiàn)位置。int

lastIndexOf(String

str,intlastIndex);返回子串str在該字符串中索引lastIndex之前最后一次出現(xiàn)位置。int

lastIndexOf(char

ch);

返回字符ch在該字符串中最后一次出現(xiàn)位置。int

lastIndexOf(String

str,intlastIndex);返回字符ch在該字符串中索引lastIndex之前最后一次出現(xiàn)位置。2023/9/738String對(duì)象的操作-字符串訪(fǎng)問(wèn)String

s

=

"JoJo

JoJo

Hooooo!";System.out.println(s.indexOf('J',4));System.out.println(s.lastIndexOf('J',4));s.indexOf('J',4);JoJo

JoJo

Hooooo!Start

hereFind

the

first

occurrence

of

Js.lastIndexOf('J',4);JoJo

JoJo

Hooooo!Start

hereFind

the

first

occurrence

of

JOutput:522023/9/7字符串訪(fǎng)問(wèn)-課后ExerciseString

funStr=

"Java

Jives";String

newStr

=

funStr.replace('J',

'W');int

jInd1a

=

funStr.indexOf('J');int

jInd1b

=

funStr.indexOf('J',

1);int

jInd2a

=

funStr.lastIndexOf('J');int

jInd2b

=

funStr.lastIndexOf('J',

4);System.out.println(jInd1a);System.out.println(jInd1b);System.out.println(jInd2a);System.out.println(jInd2b);String

banner

="One

man,

OneVote";int

subInd1a

=banner.indexOf("One");

int

subInd1b

=banner.indexOf("One",

3);int

subInd2a

=banner.lastIndexOf("One");

intsubInd2b

=banner.lastIndexOf("One",

8);System.out.println(subInd1a);System.out.println(subInd1b);System.out.println(subInd2a);Output:0

5

5

0

0

9

9

0System.out.println(subInd2b);39String對(duì)象的操作-字符串的修改2023/9/740String對(duì)象的修改(實(shí)際上產(chǎn)生一個(gè)具有新的字符串內(nèi)容的

String對(duì)象副本)String

concat(String

str);

將該字符串與str字符串相連接,產(chǎn)生一個(gè)新的String字符串對(duì)象,和String的+操作等價(jià)String

replace(char

oldChar,char

newChar);

產(chǎn)生一個(gè)新的String字符串對(duì)象,將該字符串中所有oldChar字符替換為newChar字符串中字符大小寫(xiě)轉(zhuǎn)換String

toLowerCase();生成一個(gè)新的String字符串對(duì) 象,將原字符串中所有字符轉(zhuǎn)為小寫(xiě)String

toUpperCase();

將原字符串中所有字符轉(zhuǎn)為大 寫(xiě)String對(duì)象的操作-字符串的修改E.g.: String

str

=

“hello”;str

=

str.toUpperCase();oldString

hello

”newString

“HELLO”Note:雖然為同一個(gè)引用,但是轉(zhuǎn)換后對(duì)象占用的內(nèi)存空間不同。str2023/9/741String對(duì)象的操作-字符串的修改E.g.: String

str

=

“hello”;str

=

str.toLowerCase();oldString

hello

”Note:因?yàn)樽址膬?nèi)容沒(méi)有發(fā)生改變,所以轉(zhuǎn)換之后仍然是指向原有的對(duì)象空間。str2023/9/742String對(duì)象的操作-字符串的比較(1)2023/9/743字符串的比較(String對(duì)象)區(qū)分大小寫(xiě)比較字符串內(nèi)容,boolean

equals(String

str);不區(qū)分大小寫(xiě)比較字符串內(nèi)容,boolean

equalsIgnoreCase(String

str)int

compareTo(String

other);

如果按照字典順序,字串位于other之前,就返回負(fù)值,如果字串位于other之后就返回正直,如果相等返回0;boolean

endsWith(String

suffix);

如果以suffix結(jié)尾返回true。boolean

startsWith(String

prefix);如果以prefix開(kāi)始返回true。String對(duì)象的操作-字符串的比較(2)2023/9/744等號(hào)“==“不能夠比較兩個(gè)字符串是不是相等Example:String

string1

=

“foo”;String

string2

=

string1;if(string1

==

string2)//the

result

is

True;String

string1

=

“foo”;String

string2

=new

String(

“foo”);if(string1

==

string2)//the

result

is

FalseString

string1

=

“foo”;String

string2

=

“foo”;if(string1

==

string2)//the

result

is

ture//匿名的String對(duì)象內(nèi)容相同,占據(jù)相同的空間String對(duì)象的操作-字符串的比較(3)2023/9/745“==”在字符串比較時(shí)的含義:如果string1和string2

指向同一個(gè)內(nèi)存空間,返回為真。如果string1和string2指向不同的內(nèi)存空間,無(wú)論內(nèi)容是否相等返回為假.可見(jiàn),“==”也不能夠幫我們比較兩個(gè)book對(duì)象的內(nèi)容是否相等。String對(duì)象的操作-字符串的比較(4)2023/9/746應(yīng)該怎么樣比較字符串的內(nèi)容呢?你可以用字符串類(lèi)內(nèi)置的方法可以用來(lái)比較兩個(gè)字符串的內(nèi)容是否相等:Stringstring1=“foo”;Stringstring2=new

String(

“foo”);if(string1.equals(string2))System.out.println(“Yes”);String

string1

=

“foo”;String

string2

=“FOO”;if(string1.equalsIgnoreCase(string2))System.out.println(“Yes”);以上兩個(gè)表達(dá)式都返回true如果string1的字母順序在string2之前,則放回負(fù) 數(shù)。反之返回正數(shù),如果相等返回零。Note:字母順序是指各個(gè)字母的unicode的值。越早出現(xiàn)的字母,unicode的值就越小。2023/9/747String對(duì)象的操作-字符串的比較(5)pareTo(string2);2023/9/7

48public

class

StringTest{public

static

void

main(String[]

args){String

emptyStr

=

new

String();

//

""System.out.println("0:

"

+

emptyStr);Stringstr1

=

"You

&

me!";Stringstr2

=

"You

&

me!";String

str3

=

new

String(str2);System.out.println("1:

"

+

(str1

==

str2));

//

(1)

trueSystem.out.println("2:

"

+

str1.equals(str2));

//

(2)

trueSystem.out.println("3:

"

+

(str2

==

str3));

//

(3)

falseSystem.out.println("4:

"

+

str2.equals(str3));

//

(4)

trueSystem.out.println("5:

"

+

(str1

==

Auxiliary.str1));

//

(5)

trueSystem.out.println("6:"

+

str1.equals(Auxiliary.str1));

//

(6)true}}class

Auxiliary

{staticStringstr1="You&me!";}寫(xiě)出輸出結(jié)果String對(duì)象的操作-字符串的比較(6)2023/9/749Example

of

compareTo()String

st1

=

new

String("abba");String

st2

=

new

String("aha");int

comVal

=

pareTo(st2);System.out.println(comVal);Output:-6變量的聲明:Memory

representation(兩種方式):Note:變量在內(nèi)存中空間的分配int

primitive

=

5;String

reference

=

“Hello”;5基礎(chǔ)類(lèi)型引用HelloJava的基本類(lèi)型Java的對(duì)象2023/9/750關(guān)于字符串空間只讀的討論2023/9/751在Java中字符串對(duì)象是一類(lèi)非常特殊的對(duì)象,在 程序開(kāi)發(fā)中一定要做特殊的考慮。由于字符串對(duì)象的只讀性,字符串類(lèi)型的引用在賦值操作之后和一般的對(duì)象比如說(shuō)Book類(lèi)型的引用在賦值操作之后的結(jié)果是不同的。這些不同可能會(huì)體現(xiàn)在:字符串類(lèi)型作為屬性字符串類(lèi)型作為形參字符串類(lèi)型的賦值運(yùn)算等多個(gè)方面。在實(shí)際編程序的過(guò)程中,有關(guān)字符串對(duì)象處理無(wú)論有多么復(fù)雜,只要你把握住字符串空間的只讀性就一定能夠順利解決問(wèn)題。關(guān)于字符串空間只讀的討論String

first

=

"Roses";String

second

=

first;System.out.println(first);System.out.println(second);first="Lily";System.out.println(first);System.out.println(second);“Roses”firstsecondfirst=“Lily”;“Lily”firstsecond2023/9/752關(guān)于字符串空間只讀的討論“Roses”firstsecondfirst=“Lily”;firstsecond“Lily”“Roses”2023/9/753Note:當(dāng)字符串的值發(fā)生變化時(shí),將會(huì)創(chuàng)建新的對(duì)象空間,first引用指向了新的對(duì)象空間,而Second引用指向的內(nèi)容不變。Output:RosesRosesLilyRoses2023/9/754Example:請(qǐng)嘗試判斷字符串是否回文?例如:

madam,serres都是回文。[參考]Palindromic.javapublic

class

Palindromic{public

static

void

main(String[]

args){String

str

;int

i

=0;if(args.length

<

1){System.out.println("please

input

a

word");return;}str

=args[0];for(i=0;i<str.length()/2;i++){if(str.charAt(i)!=

str.charAt(str.length()-(i+1))){break;}}if(i

<

str.length()/2)System.out.println("the

word

is

not

Palindromic

");elseSystem.out.println("the

word

is

Palindromic

");}}Predefined

class-StringBufferStringBuffer類(lèi)也是用來(lái)表示字符串。

StringBuffer類(lèi)型的對(duì)象空間是可以修改的。StringBuffer對(duì)象的值改變時(shí),系統(tǒng)會(huì)繼續(xù)在原有的空間上添加新的內(nèi)容,或在原有空間中改變內(nèi)容。這時(shí)原有的空間將繼續(xù)使用。StringBuffer對(duì)象的空間大小也是可以動(dòng)態(tài)調(diào)整的,當(dāng)字符串的長(zhǎng)度超過(guò)空間長(zhǎng)度時(shí),系統(tǒng)會(huì)做調(diào)整。Sb.append(“.txt”);strmyFilemyFile.txtStringstr

=new

String(“myFile”)

1Str

=

str

+

“.txt”;

2StringBuffer

sb

=

newStringBuffer(“myFile”);1234sbmyFile.txt3

42023/9/755Predefined

class-StringBuffer2023/9/756創(chuàng)建StringBuffer對(duì)象:StringBuffer

(String

str):通過(guò)一個(gè)string對(duì)象創(chuàng)建

StringBuffer類(lèi)的對(duì)象。該對(duì)象的初始空間的大小為str的長(zhǎng)度+16。StrinBuffer():創(chuàng)建一個(gè)空的StringBuffer對(duì)象。該對(duì)象的初始空間的大小為16。StringBuffer(int

length)

:創(chuàng)建一個(gè)指定空間大小的StringBuffer對(duì)象,可容納length個(gè)字符。Predefined

class-StringBuffer2023/9/757e.g.:StringBuffer

sb1

=

new

StringBuffer(“myFile”);//sb1所占的空間大小為6+16=22個(gè)字符StringBuffer

sb2

=

new

StringBuffer();//sb2所占的空間大小為16個(gè)字符;StrinbBuffer

sb3

=

new

StringBuffer(15);//sb3所占的空間大小為15個(gè)字符;Predefined

class-StringBufferStringBuffer對(duì)象的操作:StingBuffer類(lèi)有和String類(lèi)似的操作:String

substring(int

start)String substring(int

start,

int

end)查找字符串出現(xiàn)的位置:int

indexOf(String

str)int

lastIndexOf(String

str)查找指定位置的字符:

char

charAt(int

index)StringBuffer

sb

=

new

StringBuffer("good

morning!");String

sub

=

sb.substring(0,4);char

c=sb.charAt(6);int

i

=sb.indexOf("m");goodo52023/9/758Predefined

class-StringBuffer2023/9/759獲取字符串的長(zhǎng)度:int

length()//注意:是字符串的長(zhǎng)度,而不是存儲(chǔ)空間的長(zhǎng)度。更改指定位置的字符內(nèi)容:void

setCharAt(int

index,

char

ch)//注意:此方法在String類(lèi)中部支持。StringBuffer

strBuf1

=

new

StringBuffer("javv");strBuf1.setCharAt(strBuf1.length()-1,

strBuf1.charAt(1));System.out.println(strBuf1);Output:javaPredefined

class-StringBuffer2023/9/760StringBuffer類(lèi)特有的操作(String中不支持)StringBuffer

append(char

c)

//追加字符StringBuffer

append(char[]

str)

//追加字符數(shù)組StringBuffer

append

(int

i)//追加整形對(duì)應(yīng)的String。StringBuffer

append

(String

str)//追加stringStringBuffer

insert(int

offset,

char[]

str)//在指定位置插入字符數(shù)組

StringBuffer

insert(intoffset,floatf)//在指定位置插入浮點(diǎn)型變量對(duì)應(yīng)的String。StringBuffer

insert(int

offset,

String

str)//在指定位置插入StringStringBuffer

delete(int

start,int

end)//刪除指定位置的子串StringBuffer

deleteCharAt(int

index)//刪除指定位置的字符StringBuffer

reverse()//用相反的順序來(lái)存儲(chǔ)字符串StringBuffer—TestStringBuffer.javaStringBuffer

sb

=

new

StringBuffer("good

morning!");sb.delete(4,sb.length());

//goodSystem.out.println(sb);sb.delete(0,sb.length()-1);

//dSystem.out.println(sb);sb.insert(0,"goo");sb.append("

morning");

//goodmorningSystem.out.println(sb);sb.append(1);

//good

morning1System.out.println(sb);sb.insert(4,",,,");//good,,,

morning1System.out.println(sb);sb.setCharAt(5,':');//good,:,

morning1System.out.println(sb);2023/9/761StringBuffer2023/9/762StringBuffer對(duì)象空間操作的方法:int

capacity()返回當(dāng)前對(duì)象所分配的字符空間數(shù)。void

ensureCapacity(int

minCapcity)該方法將確保當(dāng)前對(duì)象擁有至少minCapcity大小的空間。void

setLength(int

newLength)設(shè)置對(duì)象中字符串的長(zhǎng)度,如果當(dāng)前對(duì)象中字符的長(zhǎng)度大于

newLength,將會(huì)把超過(guò)長(zhǎng)度的字符丟棄,如果當(dāng)前對(duì)象中字 符的長(zhǎng)度小于newLength,將會(huì)補(bǔ)充null字符(“\u0000”);2023/9/763StringBuffer

strNum

=

new

StringBuffer();strNum.append(5);strNum.setLength(6);System.out.print(strNum);strNum.setLength(0);strNum.append(100);strNum.setLength(6);System.out.print(strNum);strNum.setLength(0);strNum.append(1000);strNum.setLength(6);System.out.println(strNum);strNum.setLength(0);strNum.append(10);strNum.setLength(6);System.out.print(strNum);strNum.setLength(0);strNum.append(11);strNum.setLength(6);System.out.print(strNum);strNum.setLength(0);strNum.append(27);strNum.setLength(6);51001000101127Output:Predefined

class-

StringTokenizer2023/9/764為什么需要StringTokenizer呢?我們來(lái)看一個(gè)實(shí) 例:public

CD

(String

name,

Stringsinger,double

price){title

=name;artist

=singer;cost

=price;}StringTonkenizerCan

work

on

this.You

got

a

line

of

data: Bohemian

L.Povarotti

13.75You

need

to

instantiate

CD

using

the

given

data.

You

wantThe

line

is

broken

up

into

tokens:

Bohemian,

L.Povarotti,13.75so

that

you

can

do

this

:CD

cd

=

new

CD(“Bohemian”,

“L.Povarotti”,

13.75,

10);Constructors

of2023/9/765StringTokenizer(1)StringTokenizer

類(lèi)位于java.util包中。構(gòu)造方法為:StringTokenizer(String

s)Instantiating

this

kind

of

object,

the

string

will be

tokenized using

white-space

asdelimiters.For

example:

“I

like

thisis”對(duì)象中包含的Token為:“I”,“l(fā)ike”,“this”.StringTokenizer(String

s,

String

delimiter)The

delimiter

is

specified

but

the

delimiter

won’t

be

returned.For

example:StringTokenizer(“I,

you,

and

her”,

“,”);對(duì)象中包含的Token為:“I”,“you”,“and

her”Constructors

of

StringTokenizer(2)2023/9/766構(gòu)造方法為:StringTokenizer

(String

s,

String

delimiter,

booleanisDelimiter)If

boolean

value

is

true,

the

delimiter

will

be

returned.For

Example:StringTokenizer(“I,

you,

and

her”,

“,”,

true);對(duì)象中包含的Token為:

“I”,

“,”,

“you”,

”,”,

“and

her”Methods

in

StringTokenizer2023/9/767public

int

countTokens();//返回對(duì)象中Token的數(shù)目public

boolean

hasMoreTokens()判斷在當(dāng)前位置之后是否還存在Tokenpublic

String

nextToken();返回下一個(gè)Token的值。一般來(lái)說(shuō):當(dāng)我們需要處理一個(gè)字符串中所有的Token時(shí),往往可以借助循環(huán)來(lái)處理。用hasMoreTokens()來(lái)作為循

環(huán)結(jié)束的條件,而nextToken()用來(lái)在循環(huán)體中獲取Token的值。Examples

of

StringTokenizer2023/9/768StringTokenizerTest.java主要討論了用不同的構(gòu)造函數(shù)產(chǎn)生StringTokenizer對(duì)象的區(qū)別,大家可以自己去閱讀代碼。CountWords.java請(qǐng)注意:hasMoreTokens()作為循環(huán)條件的使用方法。以及nextToken()

的使用方式.Date-chapter4

Using

Predefined

Classes2023/9/769Date

類(lèi)位于java.util包中,可以用來(lái)表示日期和時(shí)間.Date類(lèi)用距離00:00:00

UTC,

January

1,

1970時(shí)間點(diǎn)的毫秒數(shù)來(lái)表示,如果早于這個(gè)時(shí)間點(diǎn)就是負(fù)值反之就是正值。構(gòu)造函數(shù)public

Date()Allocates

a

Date

object

and

initializes

it

so

that

it

represents

the

timeat

which

it

was

allocated,

measured

to

the

nearest

millisecond.public

Date(longtime)Allocates

a

Date

object

and

initializes

it

to

represent

the

specifiednumber

of

milliseconds

since

the

standard

base

time

known

as

"theepoch",

namely

January

1,

1970,

00:00:00

GMT.Instance

Methods

of

Date2023/9/770public

boolean

after(Date

when)public

boolean

before(Date

when)public

boolean

equals(Object

obj)public

long

getTime()public

long

setTime(long

time)public

String

toString()將當(dāng)前的日期轉(zhuǎn)換為:dow(星期)mon(月)dd

hh:mm:ss

zzz(時(shí)區(qū))yyyy

格式說(shuō)明:在Date類(lèi)中很多方法都用Deprecated來(lái)標(biāo)示,這表示該方法在目前的JDK版本中已經(jīng)被新的方法代替。例如int

getDay();intgetHours();等方法都已經(jīng)有其他代替的方式。[實(shí)例參考]DateTest.java,GoregorianCalendar2023/9/771GregorianCalendar類(lèi)位于java.util包中,它和

Date類(lèi)型不同在于:不再用milliseconds來(lái)表示時(shí)間。它提供了更加豐富的方法來(lái)訪(fǎng)問(wèn)日期類(lèi)型的對(duì)象。GregorianCalendar繼承于Calendar類(lèi).它支持的 很多方法都是從父類(lèi)中繼承過(guò)來(lái)的。關(guān)于繼承我 們?cè)诤竺娴膶W(xué)習(xí)中會(huì)介紹。GregorianCalendar()Constructs

a

default

GregorianCalendarusing

the

current

time

in the

default

time

zone

with

the

default

locale.E.g.

gc=new

GregorianCalendar()//表示當(dāng)前時(shí)間點(diǎn)的一個(gè)對(duì)象GregorianCalendar(int

year,

int

month,

int

date)Constructs

a

GregorianCalendar

with

the

given

date

set

in

the default

time

zone

with

the

default

locale.E.g.gc=newGregorianCalendar(2007,7,10)//表示2007-7-10的一 個(gè)對(duì)象GregorianCalendar(int

year,

int

month,

int

date,

inthour,

int

minutes,

int

seconds)Constructs

a

GregorianCalendar

with

the

given

date

and

time

set for

the

default

time

zone

with

the

default

locale.2023/9/772GoregorianCalendar的構(gòu)造方法GoregorianCalendar-靜態(tài)常量2023/9/773在Calendar類(lèi)中定義了一些靜態(tài)常量:e.g.:表示月的有:JANUARY,FEBRUARY,..NOVEMBER,DECEMBER.他們的值為JAN

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論