高级语言程序设计Java.ppt
西南科技大学网络教育系列课程高级语程序设计(Java),第六章 数组与字符串,数 组,数组是用来存储一组相同类型数据的数据结构,通过整形下标(从0开始)访问数组中的每一个元素。,数组的声明 int a;String str;或 int a;String str;数组元素的类型可以是任何数据类型。,数组的创建 使用new 如:a=new int100;/下标从0到99 数组的声明和创建可以合在一起 如:int a=new int100;,数组是恒定的:数组一旦被创建,就不能再改变它的大小。数组不是静态的:数组的内存都是通过new动态分配的。,java允许使用int变量来指定数组的大小。如:int size=20;double a=new doublesize;,int a5;,错误,不能使用这种C语言的方式,数组的初始化 使用new创建一个数组时,java会自动给每一个数组元素赋一个默认的初值。整型:0,浮点型:0.0f 或 0.0d,字符:0,布尔型:false,类对象:null,在创建数组的同时,赋予初值 int a=1,2,3,4,5,6;a=new int 1,2,3,4,5,6;/匿名数组,使用数组的length属性 length是数组类中唯一的成员变量,数组被创建时系统会自动给length赋值。通过length属性得到数组的元素数目。如:for(int i=0;i a.length;i+)System.out.println(ai);,不要使用创建数组时用于指定数组大小的整数作为循环结束的条件。,for(int i=0;i 6;i+),应该使用ia.length,拷贝数组 把一个数组变量拷贝给另一个 int a=new int1,2,3,4,5,6;int b=a;b3=9;,a和b都指向相同的数组,这时,a3的值也是9,使用System类的arraycopy()方法System.arraycopy(from,fromIndex,to,toIndex,count)to数组必须有足够的空间来容纳拷贝的元素,否则造成数组越界:,public class ArrayTest public static void main(String args)int a=new int1,2,3,4,5,6;int b=new int111,222,333,444,555,666;System.arraycopy(a,2,b,1,3);for(int i=0;ib.length;i+)System.out.println(b+i+=+bi);,对数组的操作,是对下标的操作,对数组排序 使用Arrays类的sort方法 static void sort(数据类型 数组名)Arrays.sort(被排序的数组名)sort方法采用了优化的快速排序算法,import java.util.*;public class ArraySorts public static void main(String args)int i;String test=How,many,numbers,do,you,need,to,draw,?;Arrays.sort(test);for(i=0;i test.length;i+)System.out.print(testi+t);,?How do draw many need numbers to you,二维数组使用多个下标访问数组元素 二维数组的创建使用new操作是从最高维开始,分别为每一维分配内存。int a=new int35 二维数组也可以直接以初始化的方式来创建数组 int a=1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,字符串概述,Java中没有内置的字符串数据类型,而是在标准库中包含预定义的字符串类。字符串是字符的序列,字符串中字符的位置从0开始计算。,程序中需要用到的字符串可以分为两大类,一类是创建之后不会再做修改和变动的字符串常量String类;另一类是创建之后允许再做更改和变化的字符串变量StringBuffer类。StringTokenizer类侧重于对字符串的分割。,String类,Java使用java.lang包中的String类来创建一个字符串变量,因此字符串变量是类类型变量,是一个对象。,字符串常量 使用“”定义字符串,使用定义字符。Java会自动为字符串常量生成一个String类的对象,所以可以直接初始化String对象,如:string s=“Hello World!”,创建字符串对象 使用String类,可以很方便的创建一个字符串对象。格式:String 字符串对象名=new String(“字符串值”);,String s=new String(we are students);String tom=String(s);,String类有11个构造方法,详细情况可以参看JDK的帮助文档(jdk-1_5_0-doc.zip)。下载地址:,String是恒定的:所有操作都不会改变原String对象,是将操作的结果以一个新的对象返回。,public class Stringer Static String upcase(String s)return s.toUpperCase();public static void main(String args)String q=new string(“hello”);System.out.println(q);String qq=upCase(q);System.out.println(qq);System.out.println(q);,输出:hello,输出:HELLO,输出:hello,String类的常用方法,String str1=“hello,java!”;System.out.println(str1.length();,String str2=“你好,java!”;System.out.println(str2.length();,输出:12,输出:8,public int length():获取字符串的长度。,字符串比较,比较过程:如果当前字符串与s完全相同,该方法返回值0;如果当前字符串中某个位置上的字符大于s字符串中对应位置的字符,compareTo方法返回正值,反之则compareTo方法返回负值。,用compareTo()方法,按字典顺序比较 public int compareTo(String s)多数情况下比较字符在Unicode编码表中的位置。字符串长度不等且其中一字符串是另一字符串的子串时,则比较长度。,public class CompareTest public static void main(String args)String str1=new String(helloworld);String str2=new String(hello);System.out.println(pareTo(str2);String str3=new String(helle);System.out.println(pareTo(str1);System.out.println(pareTo(str3);String str4=new String(Helle);System.out.println(pareTo(str4);,输出:5,输出:-10,输出:10,输出:32,不要用“=”来检测两个字符串是否相等,它只能判断两个字符串是否存储在同一位置。equals()方法用来判断两个字符串是否相同。public boolean equals(String s),字符串连接 使用concat()方法 public String concat(String str)使用符号+,提取部分字符串,charAt()方法:提取index位置上的一个字符。public char charAt(int index),subString()方法:获取子串public String substring(intbeginIndex)public String substring(intbeginIndex,intendIndex),String str=new String(“hello”);str=str.subString(0,4)+”!”System.out.println(str);String str=new String(“helloworld”);str=str.subString(0,5)+”+str.subString(6)+”!”System.out.println(str);,结果:hell!,结果:hello world!,去掉字符串前后的空格 public String trim(),String str3=“1 2 3 System.out.println(str3.trim();,输出:1 2 3,字符串与基本数据的相互转换“数字”格式的字符串可以转化为相应的基本数据类型。Byte、Short、Integer,Long、Float、Double类调相应的类方法来实现转换。,public static byte parseByte(String s)public static short parseShort(String s)public static int parseInt(String s)public static long parseLong(String s)public static double parseDouble(String s)public static double parseDouble(String s),String str=“1234”int n=Integer.parseInt(str);,n的值为1234,使用String类的类方法valueOf()将数字转化为字符串。,public String valueOf(byte n)public String valueOf(int n)public String valueOf(long n)public String valueOf(float n)public String valueOf(double n),double num=1234.56;System.out.println(String.valueOf(num);,输出为1234.56,对象的字符串表示 Object类有一个public 方法toString(),一个对象通过调用该方法可以获得该对象的字符串表示。,public class DataToString public static void main(String args)DataToString obj=new DataToString();System.out.println(obj.toString();System.out.println(Integer.toString(50);System.out.println(new Double(50.123).toString();,DataToStringad3ba4,50,50.123,StringBuffer类,StringBuffer类能创建可修改的字符串序列。,StringBuffer类的构造方法 StringBuffer():创建空的StringBuffer对象,初始化缓冲区为16字符的长度。StringBuffer(int len):创建指定字符缓冲区长度len的StringBuffer对象。StringBuffer(String str):创建具有初值为字符串str的StringBuffer对象,除去给定的字符串str所占的空间外,还提供16个字符的空间。,StringBuffer类的常用方法 获取StringBuffer对象的长度与容量信息。public int length():获得当前字符串缓冲区中所包含的字符数。public int capacity():获得当前字符串缓冲区的容量大小。,public class BufferStrings public static void main(String args)StringBuffer strb1=new StringBuffer();StringBuffer strb2=new StringBuffer(25);StringBuffer strb3=new StringBuffer(This is java!);System.out.print(strb1.length()+t);System.out.print(strb1.capacity()+n);System.out.print(strb2.length()+t);System.out.print(strb2.capacity()+n);System.out.print(strb3.length()+t);System.out.print(strb3.capacity()+n);,0160251430,StringBuffer对象的字符串扩充 append方法可以将其它Java类型数据转化为字符串后,再追加到StringBuffer对象中。insert方法将一个字符串插入另一个字符串中,并返回当前对象的引用。,无论是扩充还是插入字符串,只要字符串缓冲区的长度充足,缓冲区的长度就不会变。当附加或插入字符串而使得字符串缓冲区容量不足时,系统会自动分配更多的空间。,public char charAt(int index)得到参数n指定的置上的单个字符。n的值必须是非负的,并且小于当前对象实体中字符串序列的长度。,public void setCharAt(int index,char ch)将当前StringBuffer对象实体中的字符串位置n处的字符用参数ch指定的字符替换。n的值必须是非负的,并且小于当前对象实体中字符串序列的长度。,StringBuffer delete(int startIndex,int endIndex)从当前StringBuffer对象实体中的字符串中删除一个子字符串,并返回当前对象的引用。要删除的子字符串从startIndex到endIndex-1。,public class BufferStrings public static void main(String args)StringBuffer strb1=new StringBuffer(This is java!);StringBuffer strb2=strb1.delete(1,3);System.out.println(strb1);System.out.println(strb2);System.out.println(strb1.delete(1,3);,Ts is java!Ts is java!Tis java!,StringTokenizer 类,StringTokenizer类的构造方法 StringTokenizer(String s):为字符串s构造一个分析器,使用默认的分隔符集合(如:空格符、换行符、Tab符等)。StringTokenizer(Strings,String delim):为字符串s构造一个分析器,是用参数delim指定的字符作为分隔符。StringTokenizer(String s,String delim,boolean returnDelims):使用delim提供的参数作为分隔符,当returnDelims为true是,将分隔符作为标识返回。,使用StringTokenizer对象 String nextToken():逐个获取字符串中的语言符号(单词),每当调用nextToken()时,都将在字符串中获得下一个语言符号,通常用while循环来逐个获取语言符号。hasMoreTokens():用于控制循环,只要字符串中还有语言符号,该方法就返回true,否则放回false。countTokens():获得字符串中一共有多少个语言符号。,import java.util.*;public class Example public static void main(String args)String s=we are stud,ents;StringTokenizer fenxi=new StringTokenizer(s,);/空格和逗号做分 int number=fenxi.countTokens();while(fenxi.hasMoreTokens()String str=fenxi.nextToken();System.out.println(str);System.out.println(还剩+fenxi.countTokens()+个单词);System.out.println(s共有单词:+number+个);,正则表达式,正则表达式与模式匹配 正则表达式 一个正则表达式是含有一些具有特殊意义字符的字符串,这些特殊字符称作正则表达式中的元字符。,比如:“dok”就是一个正则表达式,其中d就是有特殊意义的元字符,代表0到9中的任何一个。,一个正则表达式也称作一个模式。和一个模式匹配的字符串称作匹配模式字符串,也称作模式匹配字符串。,字符串“9ok”和“1ok”都是模式匹配字符串,模式匹配 模式匹配就是检索和指定模式匹配的字符串。进行模式匹配的类在包中。,建立模式对象 public static Pattern compile(String regex)public static Pattern compile(String regex,int flags)如果参数patter指定的正则表达式有错,complie方法将抛出异常:PatternSyntaxException。,Pattern patter=Ppile(Ad);,参数flags可以取下列有效值:Pattern.CASE_INSENSITIVE:不区分大小写模式。Pattern.MULTILINE:多行模式,在输入序列的开头和结尾处匹配 Pattern.DOTALL:dotall模式 Pattern.UNICODE_CASE:Unicode的大小写模式。Pattern.CANON_EQ:规范等价模式。,表达式 au030A 将与字符串?匹配。默认情况下,匹配不考虑采用规范等价。,建立匹配对象模式对象调用matcher(CharSequence input)方法返回一个匹配对象(Matcher)。input:要匹配的字符序列。,用于匹配的方法public boolean find():寻找input中和patter匹配的下一子序列,如果成功该方法返回true,否则返回false。public boolean matches():判断input是否完全和patter匹配。判断从input的开始位置是否有和patter匹配的子序列。,元字符模式在正则表达式(模式)中可以使用一对方括号包括若干个字符,代表方括号中的任何一个字符将用于匹配。,例如:patter=“159ABC”,那么“1ABC”、“5ABC”和“9ABC”都是和模式patter匹配的字符序列。,方括号模式的一些意义如下:abc:a、b、c中的任何一个。abc:a、b、c以外的任何字符。a-d:a至d中的任何一个。,代表单个字符的元字符,