数组与字符串(基础篇).ppt_第1页
数组与字符串(基础篇).ppt_第2页
数组与字符串(基础篇).ppt_第3页
数组与字符串(基础篇).ppt_第4页
数组与字符串(基础篇).ppt_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六章 数组与字符串,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 数组,数组的使用(一维数组和二维数组;基本数据类型和复合数据类型) 数组元素表示: 数组名下标, 数组名下标1下标2 , 数组名0数组名n-1 Length域 一元数组元素的复制 =; System.arraycopy(from fromIndex,to,toIndex,count),6.1 数组示例1,/ InitArray.java: initializing an array import 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 数组示例2,/ InitArray.java import 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.java import java.io.*; public class InitArray public static void main( String args ) final int ARRAY_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.java import 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 + “ “ ); 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 数组示例4,/ JavaArrayUse.java public 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 = youngi-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.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.java public 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 = 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 = This is a string ; 字符串常量: hello world ,例1:构造方法,/ StringConstructors.java public 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 String( 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 StringBuffer(); 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 = str.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.insert(4,“abc”); str.setChatAt(int,char); 字符串的转化 StringBuffer:toString():将可变字符串变成不变字符串 String:valueOf():将不同类型的数字转化为不变字符串 字符串的重载: +,例3:方法使用,/ StringCompare.java public 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 String( “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 “hello“n “); else System.out.print( “s1 does not equal “hello“n “); if ( s1 = “hello“ ) System.out.print( “s1 equals “hello“n “); else System.out.print( “s1 does not equal “hello“n “);,例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 ) ) System.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 class Test String 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); ,内容要点,数组的创建及使用 字符串的创建及使用,习题,随机产生十个数进行降序排序。 修改例子中的冒泡排序,以提高性能。 命令行参数的使用:从命令行输入需要排序的个数,将随机产生的数进行升序排序。 编写一个应用程序,读入 如07/21/1999格式的日期,打印出如July 21,1999格式的日期。 拼写检查器。,/ StringTest.java public 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(“January “+ 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(“

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论