




已阅读5页,还剩28页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第六章数组与字符串,6.1数组6.2字符串,6.1数组,数组的创建(一维数组和二维数组;基本数据类型和复合数据类型)指定数组名称、数据类型typevar_name;如:chars;Objecto;inti;,6.1数组,为数组分配内存空间var_name=newtypesize;如:s=newchar30;o=newObject2;o0=newObject();o1=newObject();i=newint23;chars=newchar30;初始化inti1=1,1,2,3,6.1数组,数组的使用(一维数组和二维数组;基本数据类型和复合数据类型)数组元素表示:数组名下标,数组名下标1下标2,数组名0数组名n-1Length域一元数组元素的复制=;System.arraycopy(fromfromIndex,to,toIndex,count),6.1数组示例1,/InitArray.java:initializinganarrayimportjava.io.*;publicclassInitArraypublicstaticvoidmain(Stringargs)intn=newint10;for(inti=0;in.length;i+)System.out.print(i+t+ni+n);,6.1数组示例2,/InitArray.javaimportjava.io.*;publicclassInitArraypublicstaticvoidmain(Stringargs)intn=32,27,64,18,95,14,90,70,60,37;for(inti=0;in.length;i+)System.out.print(i+t+ni+n“);,6.1数组示例3,/InitArray.javaimportjava.io.*;publicclassInitArraypublicstaticvoidmain(Stringargs)finalintARRAY_SIZE=10;intn=newintARRAY_SIZE;for(inti=0;in.length;i+)ni=2+2*i;for(inti=0;in.length;i+)System.out.print(i+t+ni+n“);,/BubbleSort.javaimportjava.applet.*;publicclassBubbleSortextendsAppletpublicvoidinit()inta=2,6,4,8,10,12,89,68,45,37;for(inti=0;i0;pass-)for(inti=0;ibi+1)swap(b,i,i+1);,6.1数组示例4,/JavaArrayUse.javapublicclassJavaArrayUsepublicstaticvoidmain(Stringargs)inti,j;intyoungMaxLevel=15;intyoung;young=newintyoungMaxLevel;for(i=0;iyoung.length;i+)youngi=newinti+1;young00=1;,6.1数组示例4(杨辉三角型),for(i=1;iyoung.length;i+)youngi0=1;for(j=1;jyoungi.length-1;j+)youngij=youngi-1j-1+youngi-1j;youngiyoungi.length-1=1;for(i=0;iyoung.length;i+)for(j=0;jyoungi.length;j+)System.out.print(youngij+);System.out.println();,6.1数组示例4,6.1数组示例6(读程序),importjava.applet.Applet;publicclassPassArrayextendsAppletpublicvoidinit()inta=1,2,3,4,5;for(inti=0;ia.length;i+)System.out.print(ai+);System.out.println();modifyArray(a);for(inti=0;ia.length;i+)System.out.print(ai+);System.out.println();,6.1数组示例6(读程序),modifyElement(a3);System.out.print(a3);publicvoidmodifyArray(intb)for(intj=0;jb.length;j+)bj*=2;publicvoidmodifyElement(inte)e*=2;,6.1数组示例7,/Test.javapublicclassTestpublicstaticvoidmain(Stringargs)Pointpoint=newPoint(7,11);Circlecircle=newCircle(3.5,22,8);Cylindercylinder=newCylinder(10,3.3,10,10);ShapearrayOfShapes;arrayOfShapes=newShape3;arrayOfShapes0=point;,6.1数组示例7,arrayOfShapes1=circle;arrayOfShapes2=cylinder;for(inti=0;iarrayOfShapes.length;i+)System.out.println(nn+arrayOfShapesi.getName()+:+arrayOfShapesi.toString(),6.2字符串,创建字符串使用字符串,1.创建字符串,方法1:new方法Stringstr=newString();Stringstr=newString(Thisisastring);StringBufferstr=newStringBuffer(10);方法2:初始化方法Stringstr=Thisisastring;Stringstr;str=Thisisastring;字符串常量:helloworld,例1:构造方法,/StringConstructors.javapublicclassStringConstructorspublicstaticvoidmain(Stringargs)charcharArray=b,i,r,t;bytebyteArray=(byte)n,(byte)e,(byte)w,(byte)r;Strings,s1,s2,s3,s4,s5,s6;s=newString(hello);s1=newString();s2=newString(s);,例1:构造方法,s3=newString(charArray);s4=newString(charArray,1,2);s5=newString(byteArray,1,2);s6=newString(byteArray);System.out.print(s1=+s1+ns2=+s2+ns3=+s3+ns4=+s4+ns5=+s5+ns6=+s6+ns7=+s7);,例2:构造方法,publicclassStringBufferConstructorspublicstaticvoidmain(Stringargs)StringBufferbuf1,buf2,buf3;buf1=newStringBuffer();buf2=newStringBuffer(10);buf3=newStringBuffer(hello);System.out.println(buf1.toString();System.out.println(buf2.toString();System.out.println(buf3.toString();,2.使用字符串,String类访问:length(),charAt(),indexof(),lastIndexof(),getChars(),getBytes()等intlen=str.length();charc=str.charAt(i);inti=str.indexOf(a);inti=str.lastIndexOf(a);修改:concat(),replace(),substring(),toLowerCase(),toUpperCase()比较:equals(),equalsIgnoreCase(),CompareTo(),RegionMatches(),2.使用字符串,StringBuffer访问:length(),charAt(),getChars(),capacity();intcapa=str.capacity();修改:append(),insert(),setCharAt()str.append(“abc”);str.insert(4,“abc”);str.setChatAt(int,char);字符串的转化StringBuffer:toString():将可变字符串变成不变字符串String:valueOf():将不同类型的数字转化为不变字符串字符串的重载:+,例3:方法使用,/StringCompare.javapublicclassStringComparepublicstaticvoidmain(Stringargs)Strings1,s2,s3,s4,output;s1=newString(hello);s2=newString(goodbye);s3=newString(HappyBirthday);s4=newString(happybirthday);System.out.print(s1=+s1+ns2=+s2+ns3=+s3+ns4=+s4+nn);,例3:方法使用,if(s1.equals(hello)System.out.print(s1equalshellon);elseSystem.out.print(s1doesnotequalhellon);if(s1=hello)System.out.print(s1equalshellon);elseSystem.out.print(s1doesnotequalhellon);,例3:方法使用,if(s3.equalsIgnoreCase(s4)System.out.print(s3equalss4n);elseSystem.out.print(s3doesnotequals4n);System.out.print(pareTo(s2)is+pareTo(s2)+pareTo(s1)is+pareTo(s1)+pareTo(s1)is+pareTo(s1)+pareTo(s4)is+pareTo(s4)+pareTo(s3)is+pareTo(s3)+nn);,例3:方法使用,if(s3.regionMatches(0,s4,0,5)System.out.print(First5charactersofs3ands4matchn);elseSystem.out.print(First5charactersofs3ands4donotmatchn);if(s3.regionMatches(true,0,s4,0,5)System.out.print(First5charactersofs3ands4match);elseSystem.out.print(First5charactersofs3ands4donotmatch);,例4:方法使用,publicclassTestStringmyString=“2”;publicstaticvoidmain(Stringargs)TestmyObj=newTest()myObj.stringModifier(myObj.myString);System.out.println(“”+myObj.myString);voidstringModifier(StringtheString)theString=theString+“3”;System.out.print(theString);,内容要点,数组的创建及使用字符串的创建及使用,习题,随机产生十个数进行降序排序。修改例子中的冒泡排序,以提高性能。命令行参数的使用:从命令行输入需要排序的个数,将随机产生的数进行升序排序。编写一个应用程序,读入如07/21/1999格式的日期,打印出如July21,1999格式的日期。拼写检查器。,/StringTest.javapublicclassStringTestpublicstaticvoidmain(Stringargs)Strings=newString(07/21/1999);Strings1=s.substring(0,2);intm=Integer.parseInt(s1);switch(m)case1:System.out.print(January+s.substring(3,5)+,+s.substring(6,10);break;,case2:System.out.print(February+s.substring(3,5)+,+s.substring(6,10);break;case3:System.out.print(March+s.substring(3,5)+,+s.substring(6,10);break;case4:System.out.print(April+s.subst
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年福建省石狮市部分公办学校招聘编制内教师61人考前自测高频考点模拟试题及答案详解(全优)
- 2025年台州湾新区卫生事业单位公开招聘卫技人员2人模拟试卷附答案详解(黄金题型)
- 2025年泉州永春县部分公办学校专项招聘编制内新任教师(二)模拟试卷及参考答案详解1套
- 2025年甘肃省庆阳市镇原县第二批城镇公益性岗位83人考前自测高频考点模拟试题及参考答案详解一套
- 2025年甘肃省兰州市肺科医院招聘工作人员14人模拟试卷及答案详解(夺冠)
- 2025年宿州市人才集团有限公司招募就业见习人员7人考前自测高频考点模拟试题带答案详解
- 2025年度宜昌市中心人民医院公开招录29名专业技术人员(二)考前自测高频考点模拟试题及答案详解(历年真题)
- 地铁司机个人工作总结
- 建筑公司合同评审及管理制度模版5篇
- 2025年福建省莆田市大忠门投资咨询有限公司招聘2人模拟试卷及答案详解(名师系列)
- 父亲的病鲁迅
- 食品仓储业食品安全从业人员培训
- 教育强国建设的意义与路径探索
- 关于成立特种设备安全管理机构的通知(模板)
- 食品添加剂欧盟编码纯中文版
- 课程评价课件
- 劳动关系管理XXXXs课件
- 概率论与数理统计课后答案及概率论与数理统计(第五版)习题答案
- 建筑室外围蔽板材(简化)
- GB/T 250-2008纺织品色牢度试验评定变色用灰色样卡
- GB/T 19816.2-2005涂覆涂料前钢材表面处理喷射清理用金属磨料的试验方法第2部分:颗粒尺寸分布的测定
评论
0/150
提交评论