第六章 数组与字符串 §6.1 数组 §6.2 字符串. §6.1 数组 数组的创建 (...

33
第第第 第第第第第第 §6.1 第第 §6.2 第第第

Transcript of 第六章 数组与字符串 §6.1 数组 §6.2 字符串. §6.1 数组 数组的创建 (...

第六章 数组与字符串

§6.1 数组

§6.2 字符串

§6.1 数组数组的创建 ( 一维数组和二维数组;基本数据类型和复合数据类型)1. 指定数组名称、数据类型

type var_name[];如: char s[]; Object o[]; int i[][];

§6.1 数组2. 为数组分配内存空间

var_name = new type[size]; 如: s = new char[30]; o = new Object[2]; o[0] = new Object(); o[1] = new Object(); i = new int[2][3]; char s[] = new char[30];

3. 初始化int i1[][] = {{1,1},{2,3}}

§6.1 数组数组的使用 ( 一维数组和二维数组;基本数据类型和复合数据类型)1. 数组元素表示: 数组名 [ 下标 ] , 数组名 [ 下标 1][ 下标 2] , 数组名 [0]~ 数组名 [n-1]2. Length 域3. 一元数组元素的复制 = ;System.arraycopy(from fromIndex,to,toIndex,c

ount)

§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 数组—示例 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 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.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 ]+ " " ); System.out.println( ); bubbleSort( a );

§6.1 数组—示例 4 (排序)

§6.1 数组—示例 4 for ( int i = 0; i < a.length; i++ ) System.out.print( a[ i ]+ " " ); }public void bubbleSort( int b[] ) { for ( int pass = 1; pass < b.length; pass++ ) for ( int i = 0; i < b.length - 1; i++ ) if ( b[ i ] > b[ i + 1 ] ) swap( b, i, i + 1 ); }public void swap( int c[], int first, int second ) { int hol

d; 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 < pass; i++ )

if ( b[ i ] > b[ i + 1 ] ) swap(b,i,i + 1);

} }

§6.1 数组—示例 4

// JavaArrayUse.javapublic class JavaArrayUse { public static void main( String args[] ) { int i,j; int youngMaxLevel = 15; int young[][]; young = new int[youngMaxLevel][]; for ( i = 0; i < young.length; i++ ) young[i] = new int[i+1]; young[0] [0] = 1;

§6.1 数组—示例 4( 杨辉三角型)

for ( i = 1; i < young.length; i++ ) {young[i][0] = 1; for ( j = 1; j < young[i].length-1; j++ ) young[i][j] = young[i-1][j-1]+young[i-1][j]; young[i][young[i].length-1] = 1; }for ( i = 0; i < young.length; i++ ) {for ( j = 0; j < young[i].length; j++ )

System.out.print(young[i][j]+" "); 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.javapublic 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("\n\n" + arrayOfShapes[ i ].getName() + ": " + arrayOfShapes[ i ].toString() ) } }}

§6.2 字符串

StringBuffer 类字符串 对

不变字符串

可变字符串

String 类

1. 创建字符串

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.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 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.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 String( "Happy Birthday" ); s4 = new String( "happy birthday" ); System.out.print( "s1 = " + s1 + "\ns2 = " + s2

+"\ns3 = " + s3 + "\ns4 = " + s4 + "\n\n ");

例 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 s4\n ");else System.out.print( "s3 does not equal s4\n ");System.out.print( "\ns1.compareTo( s2 ) is " +

s1.compareTo( s2 ) + "\ns2.compareTo( s1 ) is " + s2.compareTo( s1 ) + "\ns1.compareTo( s1 ) is " + s1.compareTo( s1 ) + "\ns3.compareTo( s4 ) is " + s3.compareTo( s4 ) + "\ns4.compareTo( s3 ) is " + s4.compareTo( s3 ) + "\n\n ");

例 3 :方法使用if ( s3.regionMatches( 0, s4, 0, 5 ) )

System.out.print( "First 5 characters of s3 and s4 match\n ");

else System.out.print( "First 5 characters of s3 and s4 do not match\n ");

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); } }

内容要点

数组的创建及使用字符串的创建及使用

习题

1. 随机产生十个数进行降序排序。2. 修改例子中的冒泡排序,以提高性能。3. 命令行参数的使用 : 从命令行输入需要排

序的个数 , 将随机产生的数进行升序排序。4. 编写一个应用程序,读入 如 07/21/1999

格式的日期,打印出如 July 21 , 1999格式的日期。

5. 拼写检查器。

// 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("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("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.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; } } }