Java6数组和字符串.ppt
《Java6数组和字符串.ppt》由会员分享,可在线阅读,更多相关《Java6数组和字符串.ppt(99页珍藏版)》请在三一办公上搜索。
1、学习导读 数组、字符串都是使用Java语言编制程序要经常使用的数据结构,因此,熟练掌握这些数据结构是进一步学习Java程序设计的必要阶段。通过本章的学习,应该能够熟练掌握数组、字符串的各种基本操作。,第6章 数组、字符串,本章主要讲述如下内容:数组:Java的数组属于类类型;字符串的分类;内容不可改变的字符串类String;字符串常量;内容可以改变的字符串类StringBuffer;字符串应用。,第6章 数组、字符串,教学重点与难点:,一维数组的声明和初始化数组元素的引用二维数组的声明和使用字符与字符串的区别字符串常量与String类字符串变量与StringBuffer类,6.1 数组,Jav
2、a数组是系统定义类Array 的子类;数组的分类同C/C+:一维和多维。,6.1.1 一维数组,定义数组的格式:int a;或 int a;其值是null;在定义数组时不能指定大小。a仅是一个引用,代表一个数组名,但没有对应的空间。,产生数组空间的方法有两种:在定义时直接赋值:int a=1,2,3;通过new分配空间:int a=new int10;,数组元素的引用,定义并用运算符new为之分配空间后后,才可以引用数组中的每个元素;数组元素的引用方式:arrayNameindex index为数组元素下标,可以是整型常量或整型表达式。如a3,bi,c6*i;数组元素下标从0开始;长度为n的数
3、组合法下标取值范围:0 n-1;,数组元素的引用,int a=new int10;for(int i=0;ia.length;i+)ai=i;,int a;int a;a=new int3;a0=5;a1=11;a2=6;int a=new int3;int a=new int3;a0=5;a1=11;a2=6;int a=5,11,6;,注意:Java数组名是一个引用,当将一个数组名赋值给另一个数组时,实际上是名字的复制,例如 2-5:public class Test_Arraypublic static void main(String args)int a=1,2,3,b;b=a;fo
4、r(int i=0;i3;i+)b i+;for(int i=0;i3;i+)System.out.println(ai);,6.1.1 一维数组(续),获得数组元素的个数:数组名.length;一个整形数组若仅仅采用new分配空间,而没有对其赋值,那么每个元素的值是0。,int sourceArray=2,3,8,1,14;int destArray=new intsourceArray.length;for(int i=0;isourceArray.length;i+)destArrayi=sourceArrayi;,6.1.1 一维数组(续),int a=126,167,95;int m
5、ax=getMaxValue(a);System.out.println(Max=+max);public static int getMaxValue(int array)int m=array0;for(int i=1;i m)m=arrayi;return m;Output:Max=167,int a=126,167,95;destroy(a);System.out.println(a0+a1);.public static void destroy(int array)for(int i=0;i array.length;i+)arrayi=0;Output:0 0,6.1.2 二维数
6、组,数组中的数组元素可以是基本数据类型的值,也可以是对象类型的值。由于数组也是对象,因此,数组中的每个元素还可以是一个数组。,二维数组,二维数组举例:int a=1,2,3,4,0,9,5,6,7;,声明:int x;或int x;或 int x;,6.1.2 二维数组,产生二维数组空间的方法有两种:定义时直接赋值:x=1,2,3,4;int x=1,2,3,4;通过new运算符分配空间。,通过new运算符分配空间:x=new int23;或 x=new int2;x0=new int3;x1=new int3;,二维数组形状任意,不一定是矩形。例如程序2-7:,6.1.2 二维数组,publ
7、ic class Test_Array2 public static void main(String args)int a;a=new int2;a0=new int3;a1=new int6;(a 的长度=+a.length);System.out.println(a0 的长度=+a0.length);System.out.println(a1 的长度=+a1.length);,6.2 字符串,6.2 字符串,字符串指的是字符的序列,有两种类型的字符串:java.lang包中定义了 String和StringBuffer两个类;一种是创建以后不需要改变的,称为字符串常量,在Java中,St
8、ring类用于存储和处理字符串常量。另外一种字符串是创建以后,需要对其进行改变的,称为字符串变量,在Java中,StringBuffer类用于存储和操作字符串变量。两个类都有final修饰,这样可以优化字符串的操作。,6.2.1 String类,String类的定义原型:,public final class java.lang.String extends java.lang.Object,6.2.1 字符串常量,字符串常量属于String类型;相同的字符串常量属于同一个对象,占用同一块空间,例如:,/程序6-1public class TestConstString public stat
9、ic void main(String args)String str1=Hello,str2=Hello;System.out.println(str1=str2);System.out.println(Java=Java);,6.2.2 创建String类对象,用new运算符,并调用构造函数创建这种类型的对象,常见构造函数如下:,1.public String()采用该构造函数创建一个不含字符的空对象。例如:String str=new String();,2.public String(char value)将字符数组的内容转换为字符串,并赋予新建的对象。例如:char a=J,a,v,
10、a;String str=new String(a);3.public String(char value,int offset,int count)例如:char a=J,a,v,a;String str=new String(a,1,2);/字符串str的内容是av,4.public String(String value)采用value对象的值构造一个新的string对象。例如:String str1=Java;String str2=new String(Java);System.out.println(str2);/JavaSystem.out.println(str1=str2);
11、/false,下面是创建String对象的例子:String s;/声明一个String对象,此时s的值为空。s=new String(“abc”);/为s开辟内存空间,并初始化。String s=new String(“abc”);/把上两个语句的功能合二为一。,注意采用字符串常量初始化一个String引用的问题。例如:String str=abc;等价于:char data=a,b,c;String str=new String(data);应用举例,注意程序6-2的输出结果:,public class TestString/程序6-2 public static void main(St
12、ring args)String s1=Java,s2=Java;String s3=new String(s1);(s1=s2 is+(s1=s2);/true(s1=s3 is+(s1=s3);/falses1=ABC;s2=DEFG;(s1=s2 is+(s1=s2);/falses1=s2;(s1=s2 is+(s1=s2);/true,在Java中,String类包含有50多个方法来实现字符串的各种操作,以下介绍一些我们需要经常使用的方法。,6.2.3 String类常用方法,6.2.3 String类常用方法,1.public int length():求串长。例如:String
13、str=Java;System.out.println(str.length();/4,例子:String s=Hello!;System.out.println(s.length();/6String t=“你过得可好?”;System.out.println(t.length();/6,6.2.3 String类常用方法,2.public char charAt(int index)提取指定位置上的字符。String s=“This is a Test”;char c=s.charAt(3);/s 3.public int compareTo(String anotherString)对字
14、符串内容按字典序进行大小比较。例如:,3.字符串的比较:public int compareTo(String anotherString)其比较过程实际上是两个字符串中相同位置上的字符按Unicode中排列顺序逐个比较的结果。如果在整个比较过程中,没有发现任何不同的地方,则表明两个字符串是完全相等的,compareTo方法返回0;如果在比较过程中,发现了不同的地方,则比较过程会停下来,这时一定是两个字符串在某个位置上不相同,如果当前字符串在这个位置上的字符大于参数中的这个位置上的字符,compareTo方法返回一个大于0的整数,否则返回一个小于0的整数。,String s1=“aaaa”;S
15、tring s2=“AAAA”;int a=pareTo(s2);/32 String s3=“a1”;String s4=“a3”;int a=pareTo(s2);/-2,public class SortStringArrary/程序6-3 public static void main(String args)String str,s=Computer,CHINA,world,U.S.A;int i,j,k;System.out.print(排序之前:);for(i=0;i0)k=j;/sk中存放最小的 str=si;si=sk;sk=str;/注意该行的含义System.out.pr
16、int(n排序之后:);for(i=0;is.length;i+)System.out.print(t+si);,6.2.3 String类常用方法(续),4.char toCharArray()将String对象转换到一个字符数组中,例如:String s=Hello,Java!;char a;a=s.toCharArray();for(int i=6;ia.length;i+)System.out.print(ai);/Java!,6.2.3 String类常用方法(续),5.public boolean equals(String anString)比较两个字符串对象的内容是否相等。6.
17、public boolean equalsIgnoreCase(String anotherString)以忽略大小写方式,比较两个字符串对象的内容是否相等。注意:equals()方法与“=”之间的区别。例如:,String s1=“aaaa”;String s2=“AAAA”;boolean b1=s1.equals(s2);/false boolean b2=s1.equalsIgnoreCase(s2);/true,public class ConfuseQuestion/程序6-5 public static void main(String args)String s1=java;S
18、tring s2=new String(s1);String s3=s2;System.out.println(s1.equals(s2);/true System.out.println(s2.equals(s3);/true System.out.println(s2=s3);/true,6.2.3 String类常用方法(续),7.public int indexOf(int ch)在字符串中搜索字符,返回字符在String对象中从左边起首次出现的位置。如果没有出现,返回-1。,1)public int indexOf(int ch,int fromIndex)该方法和第一种方法类似,不
19、同的地方在于,该方法从fromIndex位置向后查找,返回的仍然是字符ch在字符串第一次出现的位置。2)public int lastIndexOf(int ch)该方法和第一种方法类似,不同的地方在于,该方法从字符串的左边位置向前查找,返回的是字符ch在字符串中最后一次出现的位置。3)public int lastIndexOf(int ch,int fromIndex)该方法和第二种方法类似,不同的地方在于,该方法以fromIndex位置为截止,返回的是字符ch在字符串中最后一次出现的位置。,String str=“This is a Test!”;int a=str.indexOf(s)
20、;/3int b=str.indexOf(s,4);/6int c=str.indexOf(“is”);/2int d=str.indexOf(“is”,4);/5,String str=“This is a Test!”;int a=str.lastIndexOf(s);/12 int b=str.lastIindexOf(s,4);/3 int c=str.lastIndexOf(“is”);/5 int d=str.lastIndexOf(“is”,4);/2,字符串中子串的查找 字符串中子串的查找与字符串中单个字符的查找十分相似,可以利用String类提供的下列方法:1)public
21、int indexOf(String str)2)public int indexOf(String str,int fromIndex)3)public int lastIndexOf(String str)4)public int lastIndexOf(String str,int fromIndex),例子:String s=“Java是面向对象的语言,JavaScript是脚本语言。”;int i=s.indexOf(“语言”);System.out.println(i);/10int j=s.indexOf(“语言”,12);System.out.println(j);/26cha
22、r c=s.charAt(5);System.out.println(c);/面,6.2.3 String类常用方法(续),8.public String substring(int begin,int end)从一个大的字符串中提取一个子串,提取string对象中从begin开始,到end-1结束的子串,返回提取的子串。String s=“12345”;String s1=new String();String s2=new String();s1=s.substring(0);/12345 s2=s.substring(1,3);/23,6.2.3 String类常用方法(续),9.pub
23、lic String concat(String str)将str对象接到调用对象的后面,返回新串。例如:String s1=Hello,s2=Java,s3;s3=s1.concat(s2);System.out.println(s3);/Hello Java注意:如果参数str为空(null),则concat方法不创建新串,而仅仅返回当前串,,例子:String s=Hello;System.out.println(s.concat(World!);System.out.println(s);,结果:Hello World!Hello,6.2.3 String类常用方法(续),10.pub
24、lic String replace(char oldChar,char newChar)将String对象中所有的oldChar字符替换为newChar,返回替换后的新串。例如:String path=d:/myjava/documents;System.out.println(path.replace(/,);/结果:d:myjavadocuments public String toString()返回当前字符串对象本身。,6.2.3 String类常用方法(续),12.public static String valueOf(各种类型 f)将各种数据类型转换成一个相应的字符串表示,该方
25、法是一个static方法。程序6-6演示了valueOf()方法的应用。程序6-7自学。,public class TestValueOf/程序6-6 public static void main(String args)char a=A,B,C,D,E,F;int i=123456;float f=3.14159f;boolean b=true;Object o=null;System.out.println(String.valueOf(a);System.out.println(String.valueOf(a,2,3);System.out.println(String.valueO
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java6 数组 字符串

链接地址:https://www.31ppt.com/p-6509255.html