1 / 63

第 2 章 字符串 数组

第 2 章 字符串 数组. 教学内容: 数组的基本概念 数组元素的访问 栈内存和堆内存 多维数组 字符串 重点: 数组的定义与访问 难点: 栈内存和堆内存 多维数组的定义与应用 学时: 2. 2.6 字符串. 字符串常量,包括直接字符串常量和 String 类的对象。字符串常量的值一旦创建不会再变动。 字符串变量,指的是 StringBuffer 类的对象。创建字符串变量的值之后允许对其进行扩充、修改。. C 中:字符数组 结尾

Télécharger la présentation

第 2 章 字符串 数组

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第2章 字符串 数组 教学内容: 数组的基本概念 数组元素的访问 栈内存和堆内存 多维数组 字符串 重点: 数组的定义与访问 难点: 栈内存和堆内存 多维数组的定义与应用 学时:2

  2. 2.6 字符串 • 字符串常量,包括直接字符串常量和String类的对象。字符串常量的值一旦创建不会再变动。 • 字符串变量,指的是StringBuffer类的对象。创建字符串变量的值之后允许对其进行扩充、修改。

  3. C 中:字符数组 结尾 \0 • char s1[ ] =“abcd”; • Java • final 类 String :不变字符串 • final 类 StringBuffer :可变字符串 • 字符串被当作对象来处理 • String类: 创建之后不能再做修改的字符串常量 • StringBuffer类:创建之后允许再做更改的字符串变量

  4. 2.6.1 String • 1 字符串常量 • 使用“”定义字符串 (‘’定义字符)。 • “abc”,”欢迎使用Java”  是字符串。 • ‘a’,’b’,’c’      是字符。 • String s1 = “上海师范大学”;

  5. 声明String变量 格式一: String <变量名>; <变量名> = new String(”字符串”); String s; //声明字符串型引用变量s,此时s的值为null s=new String(”Hello”); //在堆内存中分配空间,并 将s指向该字符串首地址 格式二: String <变量名>=new String(”字符串”); 如:String s=new String(”Hello”); 格式三: String <变量名>=”字符串”; 如:String s=”Hello”

  6. String s1; • s1 = “abcd”+“efg”; • s1 = “ab” + 4.5 ;

  7. 2 String类的常用方法 调用Java定义的格式: <字符串变量名>.<方法名>;

  8. String类的常用方法

  9. 1 public int length( ):返回字符串长度(注意汉字) String s1=“abcd”; String s2=“上海师范大学”; System.out.println( s1.length() ); • 2 boolean toUpperCase(); String s1="abcAA" ,s2 ; s2 = s1.toUpperCase(); //s1 改变了吗? System.out.println(s2 );

  10. Character 类: • boolean isUpperCase( char ); • char c1='a'; • boolean b1; • b1 = Character.isUpperCase(c1); • System.out.print(b1);

  11. 3 检索 • 查找字符或字符串, • 找到: 返回字符所在位置,(0 到 length -1 ) • 找不到: -1 • int indexOf(int ch) • int indexOf(int ch , int fromIndex) • int indexOf(String str) • int indexOf(String str, int fromIndex) String s1; s1 = new String(“abc12bcd”); int nPos1,nPos2,nPos3,nPos4; nPos1 = s1.indexOf( ‘a’); nPos2 = s1.indexOf(‘b’,2); nPos3 = s1.indexOf( “bc”); nPos4 = s1.indexOf(“bc”,2);

  12. 4 取字符串的字串或字符 • char charAt(int index) //index 从 0开始 • String substring(int beginIndex) • String substring(int beginIndex,int endIndex) • 从 beginIndex 下一个位置开始到 endIndex 的字串 String s1 char c1; s1 = new String(“abc12bcd”); c1 = s1.charAt( 3); String ss1 ,ss2; ss1 = s1.substring(5); ss2 = s1.substring (3,6);

  13. 例: String s1=“abc12bcd”; String s2 , s3 , s4 , s5,s6; s2 = s1.toUpperCase(); System.out.print(s1); //?? System.out.print(s2); s3 = s1.replace(‘b’,’H’); s4 = s1.replaceAdll(“bc”,’HK’); s5 = “ abc “; s6 = s5.trim() ; s6 = s5 + “china” 4字符串的修改 • 切记:不能直接修改String字符串本身,而是生成一新的字符串 • String toLowerCase() • String toUpperCase() • String replace(char oldChar,char newChar) • String replaceAll(String rx , String replacement) • String trim()

  14. 5 字符串比较: • == 是否同一个引用 (地址是否相同) • boolean equals(Object anObject): true/false • int compareTo(String anotherString) • 相等 0 , 大于 1 , 小于 -1 String s1 =“abcd”; String s2 = “abcde”; string s3 = new String(“abcd”); boolean b1,b2,b3,b4; b1 = s1 ==s2; b2 = s1.equals(s2); b3 = s1 ==s3; b4 = s1.equals(s3); String s1 =“abcd” ; String s2 = “abc”; int n; n = s1.compareTo( s2);

  15. s3 s4 abcd s1 • 字符串常量:存储在字符串池中,且相同内容的字符串常量只存储一次。 • String s1 =new String( “abcd”); • String s2 = new String(“abcd”); • boolean b1 ; • b1 = s1==s2; //false abcd s2 String s3 = “abcd”; String s4 = “abcd”; boolean b2 ; b2 = s3==s4; //true s2 == s3? abcd

  16. 6字符串类与其他类型的转换 • static String valueOf(char data[]) • static String valueOf(char data[],int offset,int count) • static String valueOf(boolean b) • static String valueOf(char c) • static String valueOf(int i) • static String valueOf(long l) • static String valueOf(float f) • static String valueOf(double d) 例: int x = 245; String s1 ; s1 = String.valueOf( x );

  17. string 实例 String s1=“abcdoobc”; int len = s1.length() ; int pos1 = s1.indexof( ‘b’); int pos2 = s1.indexof( ‘bc’); int pos3 = s1.indexof( ‘b’,3); int pos2 = s1.indexof( “bc”,4); char c1 = s1.charAt( 3) string ss1 = s1.substring( 5) string ss2 = s1.substring( 4 , 6) string s6 = s1.replace(‘b’,’k’);

  18. //String 相等判断 String a1; a1 =new String(“abc”); String a2 = “abc”; System.out.print(a1==a2); boolean b1; b1 = a1.equals(a2); int k = a1.compareTo(a2); int x = 134; string aa ; aa = String.valueOf( x ); //数值型转换成字符串

  19. 【例6】判断回文字符串。 public class app5_6 { public static void main(String args[]) { String str=”rotor”; int i=0,n; boolean yn=true; //是否是回文 if (args.length>0)str=args[0]; System.out.println(“str=”+str); n=str.length(); char schar,echar; while (yn && (i<n/2)) { //算法1 schar=str.charAt(i); //返回str正数第i个位置的字符 echar=str.charAt(n-i-1); //返回str倒数第i个位置的字符 if (schar==echar) i++; else yn=false; } System.out.println(“算法1:”+yn);

  20. 【例5.6】续 String temp=””,sub1=””; for (i=0;i<n;i++) { sub1=str.substring(i,i+1); //将str的第i个字符载取下来 temp=sub1+temp; //将载下来的字符放在字符串temp 的首位置 } System.out.println(“temp=”+temp); System.out.println(“算法2:”+str.equals(temp)); } }

  21. 2.6.2 StringBuffer • StringBuffer <变量>=new StringBuffer(参数) • StringBuffer s1 = new StringBuffer(“足球”);

  22. 2 StringBuffer 方法 • 1) StringBuffer append(String str); String s1="abcde"; StringBuffer sb1 = new StringBuffer("1234"); StringBuffer sb2; sb2 = sb1.append(s1); System.out.println(sb1); System.out.println(sb2);

  23. 2)StringBuffer insert(int offset,char ch); StringBuffer insert(int offset,String str); 把ch 插入到 offset 后面 StringBuffer sb1 = new StringBuffer("1234"); StringBuffer sb2; sb2 = sb1.insert(1,'T'); System.out.println(sb1); System.out.println(sb2); 3)StringBuffer delete(int start,int end); 4) StringBuffer reverse();

  24. main()方法的参数 • public static void main( String args[ ] ){ .. } • main方法的参数是在运行程序时通过命令行来输入的,并保存到一个字符串args数组中去。 • 在命令行运行的一般形式: • java <类文件名> <字串1> <字符串2> …<字串n> • 如: java demo abcd 1234

  25. class mainDemo { public static void main( String args[ ] ){ System.out.println( args[0]); System.out.println( args[1]); } } 运行: java mainDemo aaa bbb 输出: aaa bbb

  26. 练习: 1 写出实现如下功能的代码。 String s1 = new String( “ abcd1234abab”); 1) s1中有无 “c”字符, 位置? 2)s1 中有几个ab,位置如何? 3)取出 s1 中的 “34ab”。 4)把 s1 转换成大写存入字符串 s2 5)把 s1 字符串中的ab 转换成 car,存入s3

  27. String s1="abc12 ab cars bcakmdca"; int pos=0; int nCnt =0; while (pos!=-1){ pos = s1.indexOf("c" , pos ); if (pos!=-1){ System.out.print(pos+" "); nCnt++; pos++; } } System.out.println(); System.out.println( "Cnt: "+nCnt ); 1 将 字符串 “ab12cd123ef”中的数值12,123取出并转换成整数,然后相加后输出。 2 从键盘输入两个字符串,比较是否相等,第一个字串大则输出 1,相等输出0,小于输出-1。 3 在数组中存储 10 个学生的姓名,然后查找“李开” 是否在其中。 4 查找字符串 “abc12 ab cars bcakmd”中有几个c,输出其在字符串中的位置。

  28. 5 StringBuffer s1 = new StringBuffer(“abcd”); 1) 把 s1 改为 ab测试cd. 2) 把 s1 中的 b 删除。

  29. 2.7 数组

  30. 2 数组定义int n=5;int a[n];a[0] = 3;a[n]=9; 6 String 的 Equals 方法与 == 的区别。 String s1 = new String(“abcd”); String s2 = “abcd”; String s3 = “abcd”; boolean b1 , b2; b1 = s1==s2; ? b2 = s2==s3; ?

  31. 0 0x8000 X[0] 0 X[1] X[2] 0x8000 X X[9] int a=3 ; int b=7; int x [ ] ; x = new int [10 ]; 栈与堆 a=3 PC b=7 栈底 栈程序中分配 堆 自由内存

  32. 数组的基本概念 数组就是相同数据类型的元素按一定顺序排列的集合。 数组中的每个元素都具有相同的数据类型,可以用一个统一的数组名和一个下标来惟一地确定数组中的元素。

  33. 2.7.1 一维数组 使用Java的数组一般要经过三个步骤: • 1 声明数组 • 2 创建空间 • 3 赋值

  34. int a[ ]; //C++ 如何? int [ ] a; TDate d1[ ] ; • 一、 数组定义 • 数组类型 数组名[ ]; • 或 数组类型[ ] 数组名; • 1 数组类型:简单数据类型 和 类。 • 2 定义时仅定义了对数组的引用,未对数组分配内存。 • 二、生成数组(分配内存) • 数组名 = new 数据类型 [ 数组大小 ]; int a[ ] ; a = new int [10 ]; int b = new int[9]; TDate d1[ ] ; d1 = new TDate[4];

  35. 0 0x8000 X[0] 0 X[1] X[2] 0x8000 X X[9] int a=3 ; int b=7; int x [ ] ; x = new int [10 ]; 数组定义内存示意 a=3 PC b=7 栈底 内存栈 堆存栈

  36. 2.7.1.1一维数组元素的访问 • 数组元素的引用方式: 数组名[下标] • Java数组的下标是从0开始的。 • 数组名.length 数组的元素个数 int x[ ] = new int [10]; x[0] 数组中第1个元素 x[1] 第2个元素 x[9]为第10个元素 x.length ?

  37. 【例1】声明一个一维数组,其长度为5,利用循环对数组元素进行赋值,然后再利用另一个循环逆序输出数组元素的内容。程序代码如下:【例1】声明一个一维数组,其长度为5,利用循环对数组元素进行赋值,然后再利用另一个循环逆序输出数组元素的内容。程序代码如下: public class app5_1 { public static void main(String args[ ]) { int i; int a[ ]; a=new int[5]; for(i=0;i<5;i++) a[i] = i ; for(i=a.length-1 ; i>=0; i--) System.out.println(“a[”+i+”]=”+a[i]); } }

  38. 2.7.2 一维数组的初始化及应用 1 默认初始化 2 数组初始化格式: 数据类型 数组名[ ] ={初值0,初值1,…,初值n}; 例如: int a[]={1,2,3,4,5};

  39. for(i=2;i<a.length;i++){ if (a[i]>Max){ Sec=Max; Max=a[i]; } else if (a[i]>Sec) Sec=a[i]; } System.out.println(Max); System.out.println(Sec); } } 【例2】有n个互不相同的数,不用排序求出其中的最大值和次最大值。 public class app5_2 { public static void main(String args[ ]){ int i,Max,Sec; int a[]={8,50,20,7,81,55,76,93}; if (a[0]>a[1]){ Max=a[0]; Sec=a[1]; } else { Max=a[1]; Sec=a[0]; }

  40. 练习: 1 有6个数 5,3,8,2,4,1 存入数组,然后输出最小值 2 有6个数 5,3,8,2,4,1 ,从大到小输出 3 有10个1-100之间的随机数,从小到大输出 随机数生成:Math.random() 0-1间的数 • 改错: int a[3]; a[1]=4; a[2]=8;a[3]=6;

  41. 例如: int d[] = { 5,3,8,2,4,1}; int nCnt = 6; int i , j , temp; for (i=0;i<nCnt;i++){ for( j=i+1;j<nCnt;j++) if (d[i]>d[j]){ temp = d[i]; d[i]=d[j];d[j]=temp; } } for (i=0;i<nCnt;i++) System.out.print(d[i]+" ");

  42. 2.7.3 多维数组 2.7.3.1 二维数组 声明与分配内存的格式: 数据类型 数组名[ ] [ ]; 数组名 = new 数据类型 [行数] [列数]; 或: 数据类型 数组名[ ] [ ] = new 数据类型 [行数][列数];

  43. a[0][0]=1; a[0][1]=2; a[1][0]=11; • int a[][]; • a = new int[3][5]; a.length a[0].length; a[1].length;

  44. n列 2 1 m行 6 7 8 5 10 11 9 图5.5 Java语言的二维数组不一定是矩形 Java的多维数组不一定是规则的矩阵形式,如图5.5所示。 如:int [] [] x; x=new int [3] [ ]; x[0] = new int[2]; x[1] = new int[4]; x[2] = new int[3];

  45. x[0][0] x[0][1] x[0][2] x[0][ ] int x[ ][ ] x[1][0] x[1][1] x[1][ ] x[1][ ] null 图5.6 Java中的二维数组可以看成是多个一维数组

  46. x[0] x[0][0] x[1] x[0][1] x[2] x[1][0] x[1][1] x[1][2] x[1][3] x[2][0] x[2][1] x[2][2] int [] [] x;x=new int[3][]; x[0]=new int[2]; x[1]=new int[4]; x[2]=new int[3]; x x.length x[0].length x[1].length

  47. 二维数组赋初值: 数据类型 数组名[ ][ ]={{第1行初值},{第2行初值}, { ……}, {第n+1行初值} }; int a[][] = { {1,2,3} , {4,5,6} } ; int a[][] = { {1,2} , {4,5,6} } ;

  48. 练习 int a[3]={1,2,3}; int [] x=new int[3]; x[1] = 9; x.length ? int a[2]; a[2]=3; int [][] x; x=new int[2][3]; x[0][0]=1; x[0][1]=2; x.length ? int [][] x; x=new int[2][ ]; x[1][0]=8; int [][] x={{1},{2,3},{4,5,6}; x[2][2] ? x[1].length

  49. 例4计算并输出杨辉三角形。 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 [][] data d[i][j] = d[i-1] [j-1] + d[i-1][j]

  50. public class app5_4{ public static void main(String args[ ]) { int i,j; int Level=7; int iaYong[ ][ ] =new int[Level][]; for (i=0;i<iaYong.length;i++ ) iaYong[i]=new int [i+1]; iaYong[0][0]=1; 例4计算并输出杨辉三角形。 for (i=1;i<iaYong.length;i++){ iaYong[i][0]=1; for (j=1;j< iaYong[i].length-1;j++) iaYong[i][j]=iaYong[i-1][j-1]+iaYong[i-1][j]; iaYong[i][ iaYong[i].length-1]=1; } //显示出杨辉三角形 for(i=0;i< iaYong.length;i++){ for(j=0;j< iaYong[i].length;j++) System.out.print(iaYong[i][j]+” ”); System.out.println(); } } }

More Related