数组与字符串(基础篇).ppt

上传人:牧羊曲112 文档编号:6579166 上传时间:2023-11-14 格式:PPT 页数:33 大小:313.14KB
返回 下载 相关 举报
数组与字符串(基础篇).ppt_第1页
第1页 / 共33页
数组与字符串(基础篇).ppt_第2页
第2页 / 共33页
数组与字符串(基础篇).ppt_第3页
第3页 / 共33页
数组与字符串(基础篇).ppt_第4页
第4页 / 共33页
数组与字符串(基础篇).ppt_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《数组与字符串(基础篇).ppt》由会员分享,可在线阅读,更多相关《数组与字符串(基础篇).ppt(33页珍藏版)》请在三一办公上搜索。

1、第六章 数组与字符串,6.1 数组6.2 字符串,6.1 数组,数组的创建(一维数组和二维数组;基本数据类型和复合数据类型)指定数组名称、数据类型type var_name;如:char s;Object o;int i;,6.1 数组,为数组分配内存空间var_name=new typesize;如:s=new char30;o=new Object2;o0=new Object();o1=new Object();i=new int23;char s=new char30;初始化int i1=1,1,2,3,6.1 数组,数组的使用(一维数组和二维数组;基本数据类型和复合数据类型)数组元素

2、表示:数组名下标,数组名下标1下标2,数组名0数组名n-1Length域一元数组元素的复制=;System.arraycopy(from fromIndex,to,toIndex,count),6.1 数组示例1,/InitArray.java:initializing an arrayimport java.io.*;public class InitArray public static void main(String args)int n=new int 10;for(int i=0;i n.length;i+)System.out.print(i+t+n i+n);,6.1 数组示例

3、2,/InitArray.javaimport java.io.*;public class InitArray public static void main(String args)int n=32,27,64,18,95,14,90,70,60,37;for(int i=0;i n.length;i+)System.out.print(i+t+n i+n“);,6.1 数组示例3,/InitArray.javaimport java.io.*;public class InitArray public static void main(String args)final int ARRA

4、Y_SIZE=10;int n=new int ARRAY_SIZE;for(int i=0;i n.length;i+)n i=2+2*i;for(int i=0;i n.length;i+)System.out.print(i+t+n i+n“);,/BubbleSort.javaimport java.applet.*;public class BubbleSort extends Applet public void init()int a=2,6,4,8,10,12,89,68,45,37;for(int i=0;i a.length;i+)System.out.print(a i+

5、);System.out.println();bubbleSort(a);,6.1 数组示例4(排序),6.1 数组示例4,for(int i=0;i b i+1)swap(b,i,i+1);public void swap(int c,int first,int second)int hold;hold=c first;,c first=c second;c second=hold;public void bubbleSort(int b)for(int pass=b.length-1;pass 0;pass-)for(int i=0;i b i+1)swap(b,i,i+1);,6.1 数

6、组示例4,/JavaArrayUse.javapublic class JavaArrayUse public static void main(String args)int i,j;int youngMaxLevel=15;int young;young=new intyoungMaxLevel;for(i=0;i young.length;i+)youngi=new inti+1;young0 0=1;,6.1 数组示例4(杨辉三角型),for(i=1;i young.length;i+)youngi0=1;for(j=1;j youngi.length-1;j+)youngij=you

7、ngi-1j-1+youngi-1j;youngiyoungi.length-1=1;for(i=0;i young.length;i+)for(j=0;j youngi.length;j+)System.out.print(youngij+);System.out.println();,6.1 数组示例4,6.1 数组示例6(读程序),import java.applet.Applet;public class PassArray extends Applet public void init()int a=1,2,3,4,5;for(int i=0;i a.length;i+)System

8、.out.print(a i+);System.out.println();modifyArray(a);for(int i=0;i a.length;i+)System.out.print(a i+);System.out.println();,6.1 数组示例6(读程序),modifyElement(a 3);System.out.print(a 3);public void modifyArray(int b)for(int j=0;j b.length;j+)b j*=2;public void modifyElement(int e)e*=2;,6.1 数组示例7,/Test.jav

9、apublic class Test public static void main(String args)Point point=new Point(7,11);Circle circle=new Circle(3.5,22,8);Cylinder cylinder=new Cylinder(10,3.3,10,10);Shape arrayOfShapes;arrayOfShapes=new Shape 3;arrayOfShapes 0=point;,6.1 数组示例7,arrayOfShapes 1=circle;arrayOfShapes 2=cylinder;for(int i=

10、0;i arrayOfShapes.length;i+)System.out.println(nn+arrayOfShapes i.getName()+:+arrayOfShapes i.toString(),6.2 字符串,创建字符串使用字符串,1.创建字符串,方法1:new方法 String str=new String();String str=new String(This is a string);StringBuffer str=new StringBuffer(10);方法2:初始化方法 String str=This is a string;String str;str=Thi

11、s is a string;字符串常量:hello world,例1:构造方法,/StringConstructors.javapublic class StringConstructors public static void main(String args)char charArray=b,i,r,t;byte byteArray=(byte)n,(byte)e,(byte)w,(byte)r;String s,s1,s2,s3,s4,s5,s6;s=new String(hello);s1=new String();s2=new String(s);,例1:构造方法,s3=new St

12、ring(charArray);s4=new String(charArray,1,2);s5=new String(byteArray,1,2);s6=new String(byteArray);System.out.print(s1=+s1+ns2=+s2+ns3=+s3+ns4=+s4+ns5=+s5+ns6=+s6+ns7=+s7);,例2:构造方法,public class StringBufferConstructors public static void main(String args)StringBuffer buf1,buf2,buf3;buf1=new StringBu

13、ffer();buf2=new StringBuffer(10);buf3=new StringBuffer(hello);System.out.println(buf1.toString();System.out.println(buf2.toString();System.out.println(buf3.toString();,2.使用字符串,String类访问:length(),charAt(),indexof(),lastIndexof(),getChars(),getBytes()等 int len=str.length();char c=str.charAt(i);int i=s

14、tr.indexOf(a);int i=str.lastIndexOf(a);修改:concat(),replace(),substring(),toLowerCase(),toUpperCase()比较:equals(),equalsIgnoreCase(),CompareTo(),RegionMatches(),2.使用字符串,StringBuffer访问:length(),charAt(),getChars(),capacity();int capa=str.capacity();修改:append(),insert(),setCharAt()str.append(“abc”);str.

15、insert(4,“abc”);str.setChatAt(int,char);字符串的转化 StringBuffer:toString():将可变字符串变成不变字符串 String:valueOf():将不同类型的数字转化为不变字符串字符串的重载:+,例3:方法使用,/StringCompare.javapublic class StringCompare public static void main(String args)String s1,s2,s3,s4,output;s1=new String(hello);s2=new String(good bye);s3=new Strin

16、g(Happy Birthday);s4=new String(happy birthday);System.out.print(s1=+s1+ns2=+s2+ns3=+s3+ns4=+s4+nn);,例3:方法使用,if(s1.equals(hello)System.out.print(s1 equals hellon);else System.out.print(s1 does not equal hellon);if(s1=hello)System.out.print(s1 equals hellon);else System.out.print(s1 does not equal he

17、llon);,例3:方法使用,if(s3.equalsIgnoreCase(s4)System.out.print(s3 equals s4n);else System.out.print(s3 does not equal s4n);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)Sys

18、tem.out.print(First 5 characters of s3 and s4 matchn);else System.out.print(First 5 characters of s3 and s4 do not matchn);if(s3.regionMatches(true,0,s4,0,5)System.out.print(First 5 characters of s3 and s4 match);else System.out.print(First 5 characters of s3 and s4 do not match);,例4:方法使用,public cla

19、ss TestString myString=“2”;public static void main(String args)Test myObj=new Test()myObj.stringModifier(myObj.myString);System.out.println(“”+myObj.myString);void stringModifier(String theString)theString=theString+“3”;System.out.print(theString);,内容要点,数组的创建及使用字符串的创建及使用,习题,随机产生十个数进行降序排序。修改例子中的冒泡排序,

20、以提高性能。命令行参数的使用:从命令行输入需要排序的个数,将随机产生的数进行升序排序。编写一个应用程序,读入 如07/21/1999格式的日期,打印出如July 21,1999格式的日期。拼写检查器。,/StringTest.javapublic class StringTest public static void main(String args)String s=new String(07/21/1999);String s1=s.substring(0,2);int m=Integer.parseInt(s1);switch(m)case 1:System.out.print(Janu

21、ary+s.substring(3,5)+,+s.substring(6,10);break;,case 2:System.out.print(February+s.substring(3,5)+,+s.substring(6,10);break;case 3:System.out.print(March+s.substring(3,5)+,+s.substring(6,10);break;case 4:System.out.print(April+s.substring(3,5)+,+s.substring(6,10);break;case 5:System.out.print(May+s.

22、substring(3,5)+,+s.substring(6,10);break;case 6:System.out.print(June+s.substring(3,5)+,+s.substring(6,10);break;case 7:System.out.print(July+s.substring(3,5)+,+s.substring(6,10);break;,case 8:System.out.print(August+s.substring(3,5)+,+s.substring(6,10);break;case 9:System.out.print(September+s.substring(3,5)+,+s.substring(6,10);break;case 10:System.out.print(October+s.substring(3,5)+,+s.substring(6,10);break;case 11:System.out.print(November+s.substring(3,5)+,+s.substring(6,10);break;case 12:System.out.print(December+s.substring(3,5)+,+s.substring(6,10);break;,

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号