1、System.out.print(youscore=+score); (3)数据类型包装类基本类型对应包装类booleanBooleancharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublepublic classE437 public static void main(String args )chara=a,bcDEF;for(int i=0;a.length;if(Character.isLowerCase(a) a=Character.toUpperCase(a);elseif(Character.
2、isUpperCase(a) a=Character.toLowerCase(a);+a);二、字符串(1)String类:创建不可变长的字符串 创建字符串String s1=“abc”;String s2=new String(“abc”); 比较两个字符串=和equals()方法。class TestEquals public static void main(String args)Strings1=abc;Strings2=String s3=newString(String s4=newString(System.out.println(s1=s2);System.out.print
3、ln(s2=s3);System.out.println(s3=s4);System.out.println(s1.equals(s2);System.out.println(s1.equals(s3); 字符串的其他常用方法length():字符串的长度,即字符串中字符的个数。concat(String str):字符串的连接。“+”:indexOf(int ch):在当前的字符串中查找字符ch,从前向后找。lastIndexOf(int ch): 在当前的字符串中查找字符ch,从后向前找。字符串的分析:StringTokenizer类。import java.util.*;public c
4、lass WordAnalyse StringTokenizer st=new StringTokenizer(helloeverybodywhile(st.hasMoreTokens()/判断是否有后续单词。 System.out.println(st.nextToken();/取下一个单词。public String split(Stringregex)方法:使用指定分隔符分离字符串。public class TestSplitString s=new String(boo:and:foofor(String s1:s.split(:)System.out.println(s1);(2)S
5、tringBuffer类:创建可变长的字符串 创建StringBuffer对象publicStringBuffer():创建一个空的StringBuffer对象。publicStringBuffer(int length):创建一个长度为length的StringBuffer对象。publicStringBuffer(String str):用字符串str初始化StringBuffer对象。 StringBuffer的主要方法classTestStringBufferpublic static void main(String args) StringBuffer str=newStringB
6、uffer(); 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支持同步。性能:StringBuilderStringBufferStringpublic classTestBfinal static int ttime=100000;/测试循环次数public TestB
7、()public void test(String s) long begin=System.currentTimeMillis(); for(int i=0;ttime;s+=add long over=System.currentTimeMillis(); System.out.println(操作+s.getClass().getName()+类型使用的时间为:+(over-begin)+毫秒public void test(StringBuffer s)s.append(public void test(StringBuilder s)public void test2() Strin
8、g s2=abadfString s=s2+s2+s2;操作字符串对象引用相加类型使用的时间为:public void test3()Strings=操作字符串相加使用的时间为: String s1=StringBuffer sb1=newStringBuffer(StringBuilder sb2=new StringBuilder(TestB t=new TestB();t.test(s1);t.test(sb1);t.test(sb2);t.test2();t.test3();三、Vector类Vector类实现了可扩展的对象数组。测试向量的大小及容量的变化。importjava.uti
9、l.*;public classTestCapacityVector v=new Vector();System.out.println(size=+v.size();capacity=+v.capacity();14;v.add(helloAfteradded 14 Elements(1)给向量序列尾部添加新元素(2)获取向量序列中元素(3)查找向量序列中元素(4)修改向量序列中元素(5)删除向量序列中元素(6)向量的遍历访问向量的添加、获取、修改、删除和遍历。public classTestVectorv.addElement(defxyzv(1)=+v.elementAt(1);+v.g
10、et(1);System.out.println(v);v.setElementAt(DEF,1);v.removeElementAt(1);v.size(); System.out.print(v.get(i)+ System.out.println();Iterator x=v.iterator();while(x.hasNext()System.out.print(x.next()+四、Collection API 简介在Java API中为了支持各种对象的存储访问提供了Collection(收集)系列API,Vector是这种类型的一种。(1)Collection接口及实现层次接口Co
11、llection处于Collection API的最高层次,其中定义了所有底层接口或类的公共方法,如下图所示:file:/C:/Users/SBT/AppData/Local/Temp/msohtmlclip1/09/clip_image001.gif Collection接口lboolean add(Object o):将一个对象加入到收集中。boolean contains(Object o):判断收集中是否包含指定对象。boolean isEmpty():判断收集是否为空。Iterator iterator():取得遍历访问收集的迭代子对象。boolean remove(Object o
12、):从收集中删除某对象。int size():获取对象的大小。Object toArray():将收集元素转化为对象数组。void clear():删除收集中所有的元素。 Set接口该接口是数学上集合模型的抽象,有两个特点:一是不含重复元素,而是无序。public class TestSetpublic staticvoid main(String args) HashSeth=new HashSet(); h.add(Str1 h.add(newInteger(12); h.add(newDouble(4.2); h.add(newString(); System.out.println(h
13、); List接口该接口类似于数学上数列的模型,也称序列。其特点是可含有重复元素,而且是有序的。类ArrayList内部使用基于动态数组的数据结构。类LinkedList内部使用基于链表的数据结构。对于随机访问get和set,ArrayList要优于LinkedList,因为linkedList要移动指针。对于新增和删除元素操作add和remove,LinkedList比较占优势,因为ArrayList要移动数据。 importjava.util.*;public class TestList publicstatic void main(String args)ArrayLista=new
14、ArrayList();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()+,public class ListDemo2final int N=50000;long timeList(List st)long start=System.currentTimeMillis();for(inti=0;N;Object
15、 obj=new Integer(i);st.add(obj);longstop=System.currentTimeMillis();return(stop-start);ListDemo2l=new ListDemo2();timefor ArrayList=+l.timeList(new ArrayList();time forLinkedList=+l.timeList(new LinkedList();(2)Map接口及实现层次除了Collection表示的这种单一对象数据集合,对于“”表示的数据集合在CollectionAPI提供了Map接口。Map接口及其子接口的实现层次如下图:/Users/SBT/AppData/Local/Temp/msohtmlclip1/09/clip_image002.gifimport java.u
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1