Java常用系统类Word文档格式.docx

上传人:b****5 文档编号:19811429 上传时间:2023-01-10 格式:DOCX 页数:15 大小:19.17KB
下载 相关 举报
Java常用系统类Word文档格式.docx_第1页
第1页 / 共15页
Java常用系统类Word文档格式.docx_第2页
第2页 / 共15页
Java常用系统类Word文档格式.docx_第3页
第3页 / 共15页
Java常用系统类Word文档格式.docx_第4页
第4页 / 共15页
Java常用系统类Word文档格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Java常用系统类Word文档格式.docx

《Java常用系统类Word文档格式.docx》由会员分享,可在线阅读,更多相关《Java常用系统类Word文档格式.docx(15页珍藏版)》请在冰豆网上搜索。

Java常用系统类Word文档格式.docx

System.out.print("

youscore="

+score);

}

(3)数据类型包装类

基本类型

对应包装类

boolean

Boolean

char

Character

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

publicclassE437{

publicstaticvoidmain(Stringargs[]){

chara[]={'

a'

'

b'

c'

D'

E'

F'

};

for(inti=0;

a.length;

if(Character.isLowerCase(a)){

a=Character.toUpperCase(a);

elseif(Character.isUpperCase(a)){

a=Character.toLowerCase(a);

+a);

二、字符串

(1)String类:

创建不可变长的字符串

①创建字符串

Strings1=“abc”;

Strings2=newString(“abc”);

②比较两个字符串

==和equals()方法。

classTestEquals{

publicstaticvoidmain(String[]args){

Strings1="

abc"

;

Strings2="

Strings3=newString("

Strings4=newString("

System.out.println(s1==s2);

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

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

System.out.println(s1.equals(s2));

System.out.println(s1.equals(s3));

③字符串的其他常用方法

length():

字符串的长度,即字符串中字符的个数。

concat(Stringstr):

字符串的连接。

“+”:

indexOf(intch):

在当前的字符串中查找字符ch,从前向后找。

lastIndexOf(intch):

在当前的字符串中查找字符ch,从后向前找。

字符串的分析:

StringTokenizer类。

importjava.util.*;

publicclassWordAnalyse{

StringTokenizerst=newStringTokenizer("

helloeverybody"

while(st.hasMoreTokens()){//判断是否有后续单词。

System.out.println(st.nextToken());

//取下一个单词。

publicString[]split(Stringregex)方法:

使用指定分隔符分离字符串。

publicclassTestSplit{

Strings=newString("

boo:

and:

foo"

for(Strings1:

s.split("

:

))

System.out.println(s1);

(2)StringBuffer类:

创建可变长的字符串

①创建StringBuffer对象

publicStringBuffer():

创建一个空的StringBuffer对象。

publicStringBuffer(intlength):

创建一个长度为length的StringBuffer对象。

publicStringBuffer(Stringstr):

用字符串str初始化StringBuffer对象。

②StringBuffer的主要方法

classTestStringBuffer{

publicstaticvoidmain(String[]args){

StringBufferstr=newStringBuffer();

str.append("

Hello"

Mary!

str.insert(6,"

good"

str.setCharAt(6,'

G'

str.deleteCharAt(15);

str.replace(11,15,"

Tom"

System.out.println(str);

(3)StringBuilder类(JDK5.0开始):

StringBuilder不支持同步。

StringBuffer支持同步。

性能:

StringBuilder>

StringBuffer>

String

publicclassTestB{

finalstaticintttime=100000;

//测试循环次数

publicTestB(){}

publicvoidtest(Strings){

longbegin=System.currentTimeMillis();

for(inti=0;

ttime;

s+="

add"

longover=System.currentTimeMillis();

System.out.println("

操作"

+s.getClass().getName()+"

类型使用的时间为:

+(over-begin)+"

毫秒"

publicvoidtest(StringBuffers){

s.append("

publicvoidtest(StringBuilders){

publicvoidtest2(){

Strings2="

abadf"

Strings=s2+s2+s2;

操作字符串对象引用相加类型使用的时间为:

publicvoidtest3(){

Strings="

操作字符串相加使用的时间为:

Strings1="

StringBuffersb1=newStringBuffer("

StringBuildersb2=newStringBuilder("

TestBt=newTestB();

t.test(s1);

t.test(sb1);

t.test(sb2);

t.test2();

t.test3();

三、Vector类

Vector类实现了可扩展的对象数组。

测试向量的大小及容量的变化。

importjava.util.*;

publicclassTestCapacity{

Vectorv=newVector();

System.out.println("

size="

+v.size());

capacity="

+v.capacity());

14;

v.add("

hello"

Afteradded14Elements"

(1)给向量序列尾部添加新元素

(2)获取向量序列中元素

(3)查找向量序列中元素

(4)修改向量序列中元素

(5)删除向量序列中元素

(6)向量的遍历访问

向量的添加、获取、修改、删除和遍历。

publicclassTestVector{

v.addElement("

def"

xyz"

v

(1)="

+v.elementAt

(1));

+v.get

(1));

System.out.println(v);

v.setElementAt("

DEF"

1);

v.removeElementAt

(1);

v.size();

System.out.print(v.get(i)+"

"

System.out.println();

Iteratorx=v.iterator();

while(x.hasNext())

System.out.print(x.next()+"

四、CollectionAPI简介

在JavaAPI中为了支持各种对象的存储访问提供了Collection(收集)系列API,Vector是这种类型的一种。

(1)Collection接口及实现层次

接口Collection处于CollectionAPI的最高层次,其中定义了所有底层接口或类的公共方法,如下图所示:

file:

///C:

/Users/SBT/AppData/Local/Temp/msohtmlclip1/09/clip_image001.gif

①Collection接口

booleanadd(Objecto):

将一个对象加入到收集中。

booleancontains(Objecto):

判断收集中是否包含指定对象。

booleanisEmpty():

判断收集是否为空。

Iteratoriterator():

取得遍历访问收集的迭代子对象。

booleanremove(Objecto):

从收集中删除某对象。

intsize():

获取对象的大小。

Object[]toArray():

将收集元素转化为对象数组。

voidclear():

删除收集中所有的元素。

②Set接口

该接口是数学上集合模型的抽象,有两个特点:

一是不含重复元素,而是无序。

publicclassTestSet{

publicstaticvoidmain(String[]args){

HashSeth=newHashSet();

h.add("

Str1"

h.add(newInteger(12));

h.add(newDouble(4.2));

h.add(newString("

));

System.out.println(h);

③List接口

该接口类似于数学上数列的模型,也称序列。

其特点是可含有重复元素,而且是有序的。

★ 

类ArrayList内部使用基于动态数组的数据结构。

类LinkedList内部使用基于链表的数据结构。

对于随机访问get和set,ArrayList要优于LinkedList,因为linkedList要移动指针。

对于新增和删除元素操作add和remove,LinkedList比较占优势,因为ArrayList要移动数据。

importjava.util.*;

publicclassTestList{

publicstaticvoidmain(String[]args){

ArrayLista=newArrayList();

a.add("

a.add(newInteger(12));

a.add(newDouble(4.2));

a.add(newString("

System.out.println(a);

//或者使用迭代器访问a中的元素

Iteratorp=a.iterator();

while(p.hasNext()){

System.out.print(p.next()+"

"

publicclassListDemo2{

finalintN=50000;

longtimeList(Listst){

longstart=System.currentTimeMillis();

for(inti=0;

N;

Objectobj=newInteger(i);

st.add(obj);

longstop=System.currentTimeMillis();

return(stop-start);

ListDemo2l=newListDemo2();

timeforArrayList="

+l.timeList(newArrayList()));

timeforLinkedList="

+l.timeList(newLinkedList()));

(2)Map接口及实现层次

除了Collection表示的这种单一对象数据集合,对于“”表示的数据集合在CollectionAPI提供了Map接口。

Map接口及其子接口的实现层次如下图:

/Users/SBT/AppData/Local/Temp/msohtmlclip1/09/clip_image002.gif

importjava.u

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1