java字符串及数组

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第5章 字符串
5.1 字符串 5.2 字符串的常用方法 5.3 字符串与基本数据的相互转化 5.4 对象的字符串表示 5.5 StringTokenizer类 类 5.6 符串与字符、字节数组 符串与字符、 5.7 StringBuffer类 类 5.8 正则表达式
Java的数组 的数组
数组是相同类型的数据按顺序组成的一种复合数据类型。 通过数组名加数组下标,来使用数组中的数据。下标从 0开始排序。 声明数组仅仅是给出了数组名字和元素的数据类型,要 想真正的使用数组还必须为它分配内存空间,即创建数 组。在为数组分配内存空间时必须指明数组的长度。为 一维数组分配内存空间的格式如下: 数组元素的类型[数组元素的个数 数组元素的个数]; 数组名字 = new 数组元素的类型 数组元素的个数
第一轮: 个数比较3次 第一轮: 4个数比较 次 个数比较 5 8 10 10 10 8 0 wk.baidu.com 8 10 0 10 0
上浮 沉低
第三轮: 个数比较 个数比较1次 第三轮: 2个数比较 次 x[1] 0 5 x[2] 0 5
结果: 结果: x[1] x[2] 0 5 x[3] 8 x[4] 10
5.1 字符串
public boolean startsWith(String s) public boolean endsWith(String s)
字符串对象调用startsWith(String s)方法,判 断当前字符串对象的前缀是否是参数指定的字 符串s 。 字符串对象调用endsWith(String s) 方法,判 endsWith(String 断当前字符串的后缀是否是字符串s 。
public String concat(String str)
将指定字符串联到此字符串的结尾。 如果参数字符串的长度为 0,则返回此 String 对象。否则,创建一个新的 String 对象,用来 表示由此 String 对象表示的字符序列和由参数 字符串表示的字符序列串联而成的字符序列。 “Hello, “.concat(“ java”).concat(“ World!”) “Hello” +” java”+” World”
多维数组
Int twoDim[][]=new int[4][]; twoDim[0]=new int[4]; twoDim[1]=new int[2]; twoDim[2]=new int[3]; twoDim[3]=new int[4];
//Example 7 of Chapter 2 import javax.swing.JOptionPane; public class SimpleApp9 { public static void main(String[] args) { String output = ""; short ShortArray1[] = {1,2,3,4,5,6}; short ShortArray2[] = {31,32,33,34,35,36,37,38,39}; short ResultArray[]; ResultArray = new short[ShortArray1.length+ShortArray2.length]; System.arraycopy(ShortArray1,0,ResultArray,0,ShortArray1.length); System.arraycopy(ShortArray2,0, ResultArray,ShortArray1.length,ShortArray2.length); output +="数组 数组ResultArray的元素是:\n"; 的元素是: 数组 的元素是 for(int i=0;i<ResultArray.length;i++) { output +=ResultArray[i]+","; } JOptionPane.showMessageDialog(null,output); System.exit(0); } }
例子3 例子 (效果如图2.4)
public class Example2_3 { public static void main(String args[]) { int a[]={100,200,300}; int b[]={10,11,12,13,14,15,16}; b=a; b[0]=123456; System.out.println("数组a:"+a[0]+","+a[1]+","+a[2]); System.out.println("数组b:"+b[0]+","+b[1]+","+b[2]); System.out.println("数组b的长度:"+b.length); } }
class Example5_3 { public static void main(String args[]) { String a[]={"door","apple","Applet","girl","boy"}; for(int i=0;i<a.length-1;i++) { for(int j=i+1;j<a.length;j++) { if(a[j].compareTo(a[i])<0) { String temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(int i=0;i<a.length;i++) { System.out.print(" "+a[i]); 将一个字符串数组按字典序 重新排列。 重新排列。 } } }
数组的声明 Char s[]; int a[]; Point p[]; Char[] s; int[] a; Point[] p; 创建数组 s=new char[10]; a=new int[5]; Char[] s=new char[10];
char[] s=new char[5]; s[0]='c'; s[1]='h'; s[2]='i'; s[3]='n'; s[4]='a'; int[] n={12,23,9,34,2 } ;
String name=“西南财经大学”; name.startsWith(“西南”) name.startsWith(“西财”)
public class Example5_2 { public static void main(String[] args) { int number=0; String s="student;entripy;engage,english,client"; for(int k=0;k<s.length();k++) { if(s.regionMatches(k,"en",0,2)) { number++; } } System.out.println("number="+number); } }
tom.equals(boy)的值是false,tom.equals(jerry)的值 是 true。
public boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 进行比较,不考 虑大小写。如果两个字符串的长度相等,并且 两个字符串中的相应字符都相等(忽略大小写 ),则认为这两个字符串是相等的。
例子3 例子 (效果如图2.4)
public class Example2_3 { public static void main(String args[]) { int a[]={100,200,300}; int b[]={10,11,12,13,14,15,16}; b=a; b[0]=123456; System.out.println("数组a:"+a[0]+","+a[1]+","+a[2]); System.out.println("数组b:"+b[0]+","+b[1]+","+b[2]); System.out.println("数组b的长度:"+b.length); } }
public int indexOf (String s)
字符串调用方法indexOf (String s)从当前字符串的头开始检 索字符串s,并返回首次出现s的位置。如果没有检索到字符 串s,该方法返回的值是-1。 字符串调用indexOf(String s ,int startpoint)方法从当前字符串 的startpoint位置处开始检索字符串s,并返回首次出现s的位 置。如果没有检索到字符串s,该方法返回的值是-1。 字符串调用lastIndexOf (String s)方法从当前字符串的头开始 检索字符串s,并返回最后出现s的位置。如果没有检索到字 符串s,该方法返回的值是-1。
public boolean equals(String s)
字符串对象调用String类中的equals方法,比较当前 字符串对象的实体 实体是否与参数指定的字符串s的实体 实体 实体 相同. 如:
String tom=new String( "we are students"); String boy=new String( "We are students"); String jerry= new String("we are students");
String s1=“we are student.”; String s2=“we are student.”; String s3=new String(“we are student.”); String s4=new String(“we are student.”);
s3 s1 2054 s2 2012 s4 2064
public int compareTo(String s) ( )
字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s指定的字符串比较大小。 如果当前字符串与s相同,该方法返回值0;如果当前 字符串对象大于s,该方法返回正值;如果小于s,该 方法返回负值。 String str=“abcde”; str.compareTo(“boy”) <0 str.compareTo(“aba”) >0 str.compareTo(“abcde”) ==0
public class test205 { public static void main(String[] args) { // TODO: Add your code here long[] a={1,2,3,4}; long[] b={100,200,300,400,500}; System.out.println("数组b的长度"+b.length); System.out.println("b="+b[0]); b=a; System.out.println("执行b=a后"); System.out.println("数组b的长度"+b.length); System.out.println("b="+b[0]); } }
Java使用java.lang包中的String类来创建一个 字符串变量,因此字符串变量是对象。 1.符串常量 如,“你好”,“1234.987”,“weqweo”。 2.创建字符串 使用String类的构造方法,例如: String s=new String("we are students");
5.2 字符串的常用方法
(1)public int length() length()方法可以获取一个字符串的长度 ,如: String tom= "我们是学生"; tom.length()的值5。
public char charAt(int index)
返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值在索引 0 处,第二个在索引 1 处,依此类推,这类似 于数组索引。
对数组的操作: 一、有10个元素的一维数组,求最大值,最小 值,平均值,并将数组从小到大进行排序。 (如果要通过键盘给数组赋值呢?)
排序-冒泡排序
图示: 图示: x[1] 10 10 5 1 x[2] 50 5 x[3] 8 x[4] 0
4个数比较 轮,n个数比较 轮 个数比较3轮 个数比较n-1轮 个数比较 个数比较 个数比较2次 第二轮: 3个数比较 次 第二轮: 个数比较 x[1] 5 5 5 x[2] 8 8 0 8 上浮 8 8 x[3] 0 沉低 8 00
相关文档
最新文档