Java资源库.ppt

上传人:豆**** 文档编号:33108556 上传时间:2022-08-10 格式:PPT 页数:46 大小:630KB
返回 下载 相关 举报
Java资源库.ppt_第1页
第1页 / 共46页
Java资源库.ppt_第2页
第2页 / 共46页
点击查看更多>>
资源描述

《Java资源库.ppt》由会员分享,可在线阅读,更多相关《Java资源库.ppt(46页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、4.1String4.2包裹类4.3StringBuffer4.4Math4.5Vector4.6Stack4.7ArrayList4.8日期类4.9示例:Vector应用于工资管理程序程序模块的更高可复用性一直是软件技术发展追求的目标。Java SE体系中包含了丰富的类库,掌握这些类库的使用,可以大大提高应用程序开发的效率。所以本章主要在于学习如何使用这些基本类库。在Java EE /Java ME等技术体系中,均以Java SE 中的这些类库作为基础,并发展了属于自身技术体系的一系列类库。对于这些类库的熟悉程度往往影响到应用程序的功能的强弱。4.1.1String对象的创建4.1.2字符串

2、的比较 字符串在程序中出现的频率颇高,Java中的字符串是以String对象出现的。本节将系统讲述字符串对象的创建和常用方法。在应用编程中字符串的使用频率很高,String是Java中的类,String类型的变量仍然是一个引用变量,可以通过new操作符来创建一个字符串对象,使得引用变量来引用此字符串对象。public String(Stringoriginal)表示初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列。 Pro4_1:public class Operation public static void main(String args)int number;S

3、tring computerName;computerName = new String(小精灵!);String StudentName =”张三”;System.out.println(computerName);输出:小精灵!需要判断两个字符串是否相同时,要注意使用String的equals方法public class one public static void main(String args) String a = new String(副教授);String b = 副教授;System.out.println(a.equals(b);System.out.println(a=b

4、);Pro4_2:输出结果为:truefalsepublic class Test public static void main(String args) String s1,s2;s1 = new String (Tom);s2 = new String (Tom);System.out.println(s1.equals(new String (Tom);System.out.println(s1.equals(s2);System.out.println(s1=s2);输出结果为:truetruefalse4.2.1Character4.2.2Float4.2.3Boolean4.2.

5、4Integer4.2.5字符串与数值数据间的相互转换Java的包裹类及其对应的基本数据类型为:Byte(byte)、Short(short)、Integer(int)、Long(long)、Float(float)、Double(double)、Character(char)、Boolean(boolean)。 每一个包裹类都提供了一些属性和方法,用来操作和处理它所对应的基本数据类型的值,可以根据需要来灵活使用。包裹类是不能被继承的,都被修饰为Final类。public class CharacterDemopublic static void main(String args) Chara

6、cter ch = new Character(a);char c = ch.charValue();if(Character.isUpperCase(c) System.out.println(c+是大写的!);else if(Character.isLowerCase(c) System.out.println(c+是小写的!);a是小写的!public class Testpublic static void main(String args) Float f1,f2;f1 = new Float(1.23);f2 = new Float(1.23);System.out.println

7、(f1.equals(new Float(1.23);System.out.println(f1.equals(f2);System.out.println(f1=f2);输出结果为:truetruefalsepublic class Test public static void main(String args) boolean b=false; Boolean b1=new Boolean(b);Boolean b2=b;System.out.println(b1=b2);System.out.println(b1.booleanValue()=false); 输出结果为:falsetr

8、ueInteger的构造方法Integer的对象可以包裹一个int类型的值。同样Integer对象的创建可以通过如下构造方法:public Integer(intvalue)构造一个新创建的 Integer 对象,它表示指定的 int 值。public Integer(Strings)构造一个新创建的 Integer 对象,它表示 String 参数所指示的 int 值。public class Testpublic static void main(String args)Integer i1 = new Integer(3);Integer i2 = 3;System.out.printl

9、n(i2.equals(i1);System.out.println(i2=i1);输出结果为:truefalsepublic class Test public static void main(String args) Integer a = new Integer(1);Integer b = new Integer(12345);System.out.println(a=+a+,b=+b);a=1,b=123452.Integer类的几个重要字段如:Integer.MAX_VALUEstaticintMAX_VALUE ,值为 2311 的常量,它表示 int 类型能够表示的最大值。s

10、taticintMIN_VALUE ,值为 231 的常量,它表示 int 类型能够表示的最小值。3.Integer类的一些常用方法方 法 说 明 public String toString()该对象的值的字符串表示形式。public static int parseInt(String s) throws NumberFormatException 将字符串s转换成int数 public static Integer valueOf(String s) throws NumberFormatException 将字符串s转换为一个Integer对象,该对象对应的int值与字符串一致 pub

11、lic int intValue()以 int 类型返回该 Integer 对象包含的值。public class Test public static void main(String args)int data1 = 10; / 使用Integer来包裹int数据 Integer data1Wrapper = new Integer(data1); / 直接除以3 System.out.println(data1 / 3); /强制类型转换 System.out.println(double)data1 / 3); / 获取对象的double值再除以3 System.out.println

12、(data1Wrapper.doubleValue() / 3);输出结果为:33.33333333333333353.3333333333333335public class Testpublic static void main(String args) Integer c = new Integer(123);String s = new String(456);System.out.println(c.toString();System.out.println(c.parseInt(s);System.out.println(c.valueOf(s).intValue();输出结果为:

13、123456456public static Integer valueOf(Strings) 如需要编程实现将现有学号“200901046”增1,求出新学号.现有学号属于String类型.public class Testpublic static void main(String args) String s1 = 200901046 ;Integer i= Integer.parseInt(s1.trim();String s2 = Integer.toString(i+1);System.out.println(s2); 运行结果为:200901047编程实现将现有学号“XG20090

14、1046”增1,求出新学号使用substring方法,substring方法的原型为:1.public String substring(intbeginIndex)该子字符串从指定索引处的字符开始,直到此字符串末尾2.public String substring(intbeginIndex, intendIndex)该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。public class Testpublic static void main(String args) String s1 = XG200901046;Integer i= In

15、teger.parseInt(s1.substring(2, 11);String s2 = Integer.toString(i+1);System.out.println(XG+s2); 运行结果为:XG200901047如果某个字串出现的位置并不事先确定,可以使用indexOf方法来进行字串的定位。indexOf方法的原型为:public int indexOf(Stringstr)指返回指定子字符串在此字符串中第一次出现处的索引。具体应用为:public class Testpublic static void main(String args) String s1 = XG20090

16、1046;int index = s1.indexOf(XG);Integer i= Integer.parseInt(s1.substring(index+2, 11);String s2 = Integer.toString(i+1);System.out.println(XG+s2);使用length返回字符串的长度,length方法的原型为:public int length()public class Testpublic static void main(String args) String s1 = XG200901046;int len = s1.length();Integ

17、er i= Integer.parseInt(s1.substring(2, len);String s2 = Integer.toString(i+1);System.out.println(XG+s2);StringBuffer对象表示可变字符序列。它常见的构造方法原型为:public StringBuffer(intcapacity)表示构造一个不带字符,但具有指定初始容量的字符串缓冲区。新增字符串的长度如大于原来的容量,则将(初始容量1)2的值和(新增字符串的长度初始容量)比较,如大于,则新容量为(新增字符串的长度初始容量)的值,否则为(初始容量1)2的值。public class T

18、estpublic static void main(String args) StringBuffer s1 = new StringBuffer(2);System.out.println( OrginCapacity=+s1.capacity();System.out.println(s1.length() );String str = Study;s1.append(str);System.out.println( Newcapacity=+s1.capacity();System.out.println( s1.length();OrginCapacity=20Newcapacity

19、=65Math 类包含用于执行基本数学运算的方法。如PI和E。PI是Math类 数据成员,它的原型为:public static final double PI是一个比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。E是Math类 数据成员,它的原型为:public static final double E是一个比任何其他值都更接近 e(即自然对数的底数)的 double 值。 合理的使用Math类可以提高编码的效率。Vector可以合理的管理多个对象,并方便地提供对象的查找、新增等功能。Vector的Size方法可以返回元素个数的总数方法原型为: public int

20、 size()import java.util.Vector;public class testStack public static void main(String args) Vector theVector = new Vector();for (int i=0;i10;i+)Integer newInteger = new Integer(i);theVector .addElement(newInteger);for (int k=0;ktheVector.size();k+)System.out.println(theVector.elementAt(k);0123456789i

21、mport java.util.Vector;public class testStack public static void main(String args) Vector theVector = new Vector();for (int i=0;i10;i+)Integer newInteger = new Integer(i); theVector .addElement(newInteger);theVector.add(3, new Integer(33);for (int k=0;ktheVector.size();k+)System.out.println(theVecto

22、r.elementAt(k).toString();012333456789Vector可以自动维护索引。0, 1, 2, 33, 3, 4, 5, 6, 7, 8, 9import java.util.Vector;public class Test public static void main(String args) Vector theVector = new Vector();for (int i=0;i10;i+)point val = new point(i,i*i); theVector .addElement(val);for (int k=0;ktheVector.siz

23、e();k+)System.out.println(theVector.elementAt(k).y); class point int x,y;public point(int i,int j)x=i;y=j;0149162536496481应改成:System.out.println(point)theVector.elementAt(k).y);能否使用这个集合类的时候不去这样繁琐地进行类型转换呢?import java.util.Vector;public class Test public static void main(String args) Vector theVector

24、= new Vector();for (int i=0;i10;i+) point val = new point(i,i*i); theVector .addElement(val); for (int k=0;ktheVector.size();k+)System.out.println(theVector.elementAt(k).y); class point int x,y;public point(int i,int j)x=i;y=j;01491625364964814.5中提到Stack是Vector的子类,有“后进先出”的特性,主要体现在两个方法上:pop和push。使用po

25、p方法可以方便的将栈顶的元素得到。import java.util.*;public class testStackpublic static void main(String args) point p;Stack mystack1 = new Stack();for(int i=0; i5; i+) p=new point();p.x= i; p.y=i*i;mystack1.push(p);for (int i=0;i5;i+) p=(point)mystack1.pop();System.out.println(x: +p.x+y:+p.y );class point int x,y;

26、输出结果为:x: 4y:16x: 3y:9x: 2y:4x: 1y:1x: 0y:0“后进先出”Stack mystack1 = new Stack();import java.util.*;public class testStackpublic static void main(String args) point p;Stack mystack1 = new Stack();for(int i=0; i5; i+) p=new point();p.x= i; p.y=i*i;mystack1.push(p);System.out.println(元素的个数共为: +mystack1.si

27、ze();for (int i=0;i5;i+) p=mystack1.pop();System.out.println(x: +p.x+y:+p.y );System.out.println(元素的个数共为: +mystack1.size(); class pointint x,y;输出结果为:元素的个数共为:1元素的个数共为:2元素的个数共为:3元素的个数共为:4元素的个数共为:5x: 4y:16元素的个数共为:4x: 3y:9元素的个数共为:3x: 2y:4元素的个数共为:2x: 1y:1元素的个数共为:1x: 0y:0元素的个数共为:0“后进先出”import java.util.Ar

28、rayList; public class testArraylist public static void main(String args) ArrayList theArray = new ArrayList();for (int i=0;i10;i+)Integer newInteger = new Integer(i);theArray.add(newInteger);for (int k=0;ktheArray.size();k+)System.out.println(theArray.get(k);import java.util.ArrayList;public class T

29、estpublic static void main(String args)ArrayListArrayList vArr = new ArrayListArrayList();ArrayList strV3 = new ArrayList();strV3.add(1);strV3.add(2);strV3.add(3);strV3.add(4);strV3.add(5);ArrayList strV4 = new ArrayList();strV4.add(6);strV4.add(7);strV4.add(8);strV4.add(9);vArr.add(strV3);vArr.add(

30、strV4);for(ArrayList arr : vArr)for(Integer s:arr)System.out.println(s);运行结果为:1234567891.编程题:将以下4个同学的学号、姓名如: 01,张一 02,李二 03,王三 04,赵四通过ArrayList进行存储、并遍历该ArrayList,将其中学号为“02”的学生输出其姓名。注意:程序中应设计一个学生类用于学号、姓名等属性的封装。2.程序填空题有n个孩子站成一圈,从第一个孩子开始顺时针方向报数,报到3的人出列,下一个人继续从1报数,直到最后剩下一个孩子为止。问剩下第几个孩子。下面的程序以10个孩子为例,模拟了

31、这个过程,请完善之(提示:报数的过程被与之逻辑等价的更容易操作的过程所代替)。Vector a = new Vector();for(int i=1; i=10; i+)a.add(第 + i + 个孩子);for(;)if(a.size()=1) break;for(int k=0; k2; k+)a.add(a.elementAt(0); a.remove(0);a.remove(0);System.out.println(a);a.add(a.remove(0);存在如下5个字符串:“x=4:y=16”“x=3:y=9”“x=2:y=4”“x=1:y=1”“x=0:y=0”提取其中以数字

32、形式存在的x、y数值坐标,创建其对应的点对象。并将这些点对象通过ArrayList对象来集中管理:如通过ArrayList对象输出5个点对象的x、y坐标值。且输出形式为:第1个点: 0,0第2个点: 1,1第3个点: 2,4第4个点: 3,9第5个点:4,16import java.util.*; public class testpublic static void main(String args) Calendar c = Calendar.getInstance(); System.out.println(今年是:+c.get(Calendar.YEAR )+年);System.out.println(本月月份是:+(c.get(Calendar.MONTH)+1)+月);System.out.println(今天为本月第:+c.get(Calendar.DAY_OF_MONTH)+天);c.set(Calendar.DAY_OF_MONTH,11);System.out.println(今天为本月第:+c.get(Calendar.DAY_OF_MONTH)+天);程序运行结果为:今年是:2011年本月月份是:1月今天为本月第:27天今天为本月第:11天

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com