ch04.数组

上传人:豆浆 文档编号:48543660 上传时间:2018-07-17 格式:PPT 页数:39 大小:2.23MB
返回 下载 相关 举报
ch04.数组_第1页
第1页 / 共39页
ch04.数组_第2页
第2页 / 共39页
ch04.数组_第3页
第3页 / 共39页
ch04.数组_第4页
第4页 / 共39页
ch04.数组_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《ch04.数组》由会员分享,可在线阅读,更多相关《ch04.数组(39页珍藏版)》请在金锄头文库上搜索。

1、第四章第四章 数数 组组JavaJava程序设计程序设计Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 2 2 / 39/ 39Chapter3 数 组目目 录录数组数组 Int heights = 84.124, 78.2, 61.5;boolean tired = true, false, false, true;String names =“Zhang“,“Li“,“Wang“;Lu Qiang, 2007 Hefei University of Tech

2、nology, School of Computer and Information PagePage 6 6 / 39/ 39Chapter3 数 组创建数组 静态初始化和动态初始化 动态初始化是使用运算符new为数组分配空间。数组说明的方括号中的数字表示数组元素个数:type arrayName = new typearraySizetype arrayName = new typearraySizes = new char20;Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information

3、 PagePage 7 7 / 39/ 39Chapter3 数 组数组边界 数组边界 数组下标从0开始,元素个数length是数组类中唯一的数据成员变量 new创建数组时系统自动给length赋值 数组一旦创建完毕,其大小就固定下来 程序运行时可以使用length进行数组边界检查。如果发生越界访问,则抛出一个异常Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 8 8 / 39/ 39Chapter3 数 组一维数组初始化示例第一种形式int score;sco

4、re=new int10;第二种形式int score=new int10;第三种形式int score=65,34,78,81,56,92,56,87,90,77;JAVA数组的下标最小值为0,最大值为元素个数减一。S9S8S7S6S5S4S3S2S1S065 34 78 81 56 92 56 87 90 77欲知道初始化后数组中元素的个数可通过属性length获得其格式为 : 数组名.lengthLu Qiang, 2007 Hefei University of Technology, School of Computer and Information Lu Qiang, 2007

5、Hefei University of Technology, School of Computer and Information PagePage 9 9 / 39/ 39Chapter3 数 组一维数组的复制/1ArrayCopy /一维数组的复制 package chapter4; class example04_01 public static void main(String args ) int a , b , i, j; a=new int3; b=new int5; System.out.println(“a.length=“+a.length); for (i=0;i aj

6、 ) k=j; t=ai; ai=ak; ak=t; for (i=0;inumj+1) temp=numj; numj=numj+1; numj+1=temp; for (i=0;i10;i+) System.out.print(“ ”+numi); Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 3030 / 39/ 39Chapter3 数 组快速排序对冒泡排序的一种改进基本思想: 通过一躺排序将数据分割成独立的两部分,其中一部分的所有数据都比另外一不部分

7、的所有数据都要小; 然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列“java.util”包中,专门有一个数组类Arrays,该类提供了一些方法用于排序、查找等操作,在编制程序中可以直接使用这些方法。Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 3131 / 39/ 39Chapter3 数 组目目 录录数组数组 S2=“is a student”;System.out.println(s1+s2);运算符+

8、的作用是将前后两 个字符串连接起来Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 3333 / 39/ 39Chapter3 数 组字符串例程/ 字符串应用 package chapter4; public class example04_04 public static void main (Str

9、ing args ) String s1,s2; s1=new String (“Students should “); s2=new String(); s2=“ study hard.“; System.out.print(s1); System.out.println(s2); s2=“learn English, too“; System.out.print(s1); System.out.println(s2); s2=s1+s2; System.out.println(s2); 程序运行结果Students should study hardStudents should lear

10、n English ,tooStudents should learn English ,tooLu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 3434 / 39/ 39Chapter3 数 组字符串操作访问字符串对象1.length() 返回字符串长度2.char charAt(int index)返回字符串中第index个字符3.int indexOf(int ch)返回字符串中字符ch第一次出现的位置4.int indexOf(String str,int

11、 index)返回值为,从字符串的第index位置开始,子串str第一次出现的位置5.subString(int index1 ,int index2)返回的是从字符串的第index1位置开始到index2位置结束的子串操作示例String s=new String();s=“I am a student.”;s.length()的值为15s.charAt(7)的值为ss.indexOf(a) 其值为2注意字符串从0计数找不到其值为-1s.indexOf(“stu”,0)的值为7,s.indexOf(“stu”,9)的值为-1s.subString(7,13)的值为“studen”Lu Qia

12、ng, 2007 Hefei University of Technology, School of Computer and Information PagePage 3535 / 39/ 39Chapter3 数 组字符串比较 字符在计算机中是按照Unicode编码存储的,两个字符串的比较实际上是字符串中对应字符编码的比较。 从首字符开始逐个向后比较对应字符,当发现了一对不同的 字符或到字符串末尾,两个字符串比较结束。常用的成员方法1.equals (Object obj) 本字符串与obj字符串比较,相 等返回true,不等返回false2.equalsIgnoreCase(String

13、 str) 字符串比较,忽略大小写 pareTo(String str)返回值 为整型,本字符串大于str取正 值,小于取负值,相等取0值示例String s=new String(“student”);s.equals(“Student”)的值为falses.equalsIgnoreCase(“Student”)的值为pareTo(“five student”)的值为正pareTo(“two student”)的值为负Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePa

14、ge 3636 / 39/ 39Chapter3 数 组字符串与其他类型的转换 基本类型数据转换成字符串的方法是 String.valueOf(基本数据类型) 如 StringvalueOf(123)的值为“123” StringvalueOf(0.34)的值为“0.34”方 法返回值类型返回值Boolean.getBoolean(“false”)booleanfalseInteger.parseInt(“123”)int123Long.parseLong(“375”)long375Float.parseFloat(“345.23”)float345.23Double.parse Double(“67892.34”)double67892.34Lu Qiang, 2007 Hefei University of Technology, School of Computer and Information PagePage 3737 / 39/ 39

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号