第7章对象群体的组织.ppt

上传人:wj 文档编号:809463 上传时间:2023-04-30 格式:PPT 页数:108 大小:419.50KB
下载 相关 举报
第7章对象群体的组织.ppt_第1页
第1页 / 共108页
第7章对象群体的组织.ppt_第2页
第2页 / 共108页
第7章对象群体的组织.ppt_第3页
第3页 / 共108页
第7章对象群体的组织.ppt_第4页
第4页 / 共108页
第7章对象群体的组织.ppt_第5页
第5页 / 共108页
第7章对象群体的组织.ppt_第6页
第6页 / 共108页
第7章对象群体的组织.ppt_第7页
第7页 / 共108页
第7章对象群体的组织.ppt_第8页
第8页 / 共108页
第7章对象群体的组织.ppt_第9页
第9页 / 共108页
第7章对象群体的组织.ppt_第10页
第10页 / 共108页
第7章对象群体的组织.ppt_第11页
第11页 / 共108页
第7章对象群体的组织.ppt_第12页
第12页 / 共108页
第7章对象群体的组织.ppt_第13页
第13页 / 共108页
第7章对象群体的组织.ppt_第14页
第14页 / 共108页
第7章对象群体的组织.ppt_第15页
第15页 / 共108页
第7章对象群体的组织.ppt_第16页
第16页 / 共108页
第7章对象群体的组织.ppt_第17页
第17页 / 共108页
第7章对象群体的组织.ppt_第18页
第18页 / 共108页
第7章对象群体的组织.ppt_第19页
第19页 / 共108页
第7章对象群体的组织.ppt_第20页
第20页 / 共108页
亲,该文档总共108页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

第7章对象群体的组织.ppt

《第7章对象群体的组织.ppt》由会员分享,可在线阅读,更多相关《第7章对象群体的组织.ppt(108页珍藏版)》请在冰点文库上搜索。

第7章对象群体的组织.ppt

第七章对象群体的组织,JAVA语言程序设计,目录,7.1对象数组7.3集合(Collection,Map)7.4本章小结,7.1对象数组,数组在Java提供的存储及随机访问对象序列的各种方法中,数组是效率最高的一种类型检查边界检查优点数组知道其元素的类型编译时的类型检查大小已知代价数组对象的大小是固定的,在生存期内大小不可变,对象数组数组元素是类的对象所有元素具有相同的类型每个元素都是一个对象的引用,对象数组,7.1对象数组(续),静态初始化:

在声明和定义数组的同时对数组元素进行初始化,例如:

BankAccountaccounts=newBankAccount(“Zhang,100.00),newBankAccount(“Li,2380.00),newBankAccount(“Wang,500.00),newBankAccount(“Liu,175.56),newBankAccount(“Ma,924.02);动态初始化:

使用运算符new,需要经过两步:

首先给数组分配空间typearrayName=newtypearraySize;然后给每一个数组元素分配空间arrayName0=newtype(paramList);arrayNamearraySize-1=newtype(paramList);,7.1.1用数组存储对象(续)对象数组的初始化,对象数组,使用数组存储一个班的学生信息及考试成绩。

学生信息包括学号、姓名、三门课(英语、数学、计算机)的成绩及总成绩。

首先声明学生类Student属性包括学号(id),姓名(name),英语成绩(eng),数学成绩(math),计算机成绩(comp),总成绩(sum)方法包括构造方法,get方法,set方法,toString方法,equals方法,compare方法(比较两个学生的总成绩,结果分大于,小于,等于),sum方法(计算总成绩)实现Serializable接口,以便对象持久化,,7.1.1用数组存储对象(续)例7_1,对象数组,importjava.io.*;publicclassStudentimplementsSerializableprivateStringid;/学号privateStringname;/姓名privateinteng;/英语成绩privateintmath;/数学成绩privateintcomp;/计算机成绩privateintsum;/总成绩publicStudent(Stringid,Stringname,inteng,intmath,intcomp)this.id=id;this.name=name;this.eng=eng;this.math=math;p=comp;sum();/计算总成绩,7.1.1用数组存储对象(续)例7_1,对象数组,publicStudent(Students)this.id=s.id;this.name=newString(s.name);this.eng=s.eng;this.math=s.math;p=p;sum();/计算总成绩publicvoidsetId(Stringid)this.id=id;publicvoidsetName(Stringname)this.name=name;,7.1.1用数组存储对象(续)例7_1,对象数组,publicvoidsetEng(inteng)this.eng=eng;sum();/计算总成绩publicvoidsetMath(intmath)this.math=math;sum();/计算总成绩publicvoidsetComp(intcomp)p=comp;sum();/计算总成绩publicStringgetId()returnid;,7.1.1用数组存储对象(续)例7_1,对象数组,publicStringgetName()returnname;publicintgetEng()returneng;publicintgetMath()returnmath;publicintgetComp()returncomp;publicintgetSum()returnsum;voidsum()this.sum=eng+math+comp;,7.1.1用数组存储对象(续)例7_1,对象数组,publicStringtoString()returngetId()+t+getName()+t+getEng()+t+getMath()+t+getComp()+t+getSum();publicbooleanequals(Objectx)if(this.getClass()!

=x.getClass()returnfalse;Studentb=(Student)x;return(this.getId().equals(b.getId();/比较成绩大小,当前对象成绩比参数对象成绩大时返回1,相等时返回0,其它返回-1.publicintcompare(StudentA)if(this.getSum()A.getSum()return1;elseif(this.getSum()=A.getSum()return0;elsereturn-1;,7.1.1用数组存储对象(续)例7_1,对象数组,下面声明班级类StudentClass:

属性包括班级名称(name),容量(capacity),学生(students),实际人数(size)。

方法包括构造方法,get方法,set方法,toString方法。

publicclassStudentClassprivateStringname;/班级名称staticintcapacity=40;/最大容量privateStudentstudents;/学生privateintsize;/实际人数,7.1.1用数组存储对象(续)例7_1,对象数组,publicStudentClass(Stringname,intsize)this.name=name;this.size=size;students=newStudentcapacity;publicStringgetName()returnname;publicintgetCapacity()returncapacity;publicStudentgetStudents()returnstudents;,7.1.1用数组存储对象(续)例7_1,对象数组,publicintgetSize()returnsize;publicvoidsetName(Stringname)this.name=name;publicvoidsetCapacity(intcapacity)this.capacity=capacity;publicvoidsetSize(intsize)this.size=size;publicvoidsetStudents(Studentstudents)for(inti=0;isize;i+)this.studentsi=newStudent(studentsi);,7.1.1用数组存储对象(续)例7_1,对象数组,publicStringtoString()Strings;s=班级:

+name+t+容量:

+capacity+t+实际人数:

+size+nn;s=s+“学号”+“t”+“姓名”+“t”+“英语”+“t”+“数学”+“t”+“计算机”+t+总成绩n;for(inti=0;isize;i+)s=s+studentsi.getId()+t+studentsi.getName()+t+studentsi.getEng()+t+studentsi.getMath()+t+studentsi.getComp()+t+studentsi.getSum()+n;returns;,7.1.1用数组存储对象(续)例7_1,对象数组,下面声明测试类Tester1(其中Keyboard类的声明见第3章例3-12),为测试简单,仅生成具有5名学生的班级,5名学生的信息从键盘输入,为了避免以后再重复输入,可将输入的学生信息保存到文件中importjava.io.*;publicclassTester1publicstaticvoidmain(Stringargs)Studentstudents;StudentClassaClass=newStudentClass(软件0201,5);students=newStudent5;for(inti=0;i5;i+)studentsi=newStudent(getAStudent(i+1);aClass.setStudents(students);System.out.println(aClass);,7.1.1用数组存储对象(续)例7_1,对象数组,/将学生信息保存到文件stu.ser中。

tryfo=new(stu.ser);ObjectOutputStreamso=newObjectOutputStream(fo);for(inti=0;i5;i+)so.writeObject(studentsi);so.close();catch(Exceptione)System.out.println(e);,7.1.1用数组存储对象(续)例7_1,对象数组,publicstaticStudentgetAStudent(inti)Studentstudenti;System.out.println(输入第+i+个学生的信息:

);System.out.print(学号:

);Stringid=Keyboard.getString();System.out.print(姓名:

);Stringname=Keyboard.getString();System.out.print(英语成绩:

);inteng=Keyboard.getInteger();System.out.print(数学成绩:

);intmath=Keyboard.getInteger();System.out.print(计算机成绩:

);intcomp=Keyboard.getInteger();studenti=newStudent(id,name,eng,math,comp);returnstudenti;,7.1.1用数组存储对象(续)例7_1,对象数组,运行结果如下(其中学生信息的输入只显示一部分):

输入第1个学生的信息:

学号:

250201姓名:

李红英语成绩:

88数学成绩:

76计算机成绩:

60输入第2个学生的信息:

班级:

软件0201容量:

40实际人数:

5学号姓名英语数学计算机总成绩250201李红887660224250202张林786780225250203董玉梅868075241250204张力706875213250205何为809078248,7.1.1用数组存储对象(续)例7_1运行结果,对象数组,查找也称为检索,就是从一组数据中找出所需的具有某种特征的数据项顺序查找对所存储的数据从第一项开始(也可以从最后一项开始),依次与所要查找的数据进行比较,直到找到该数据或将全部元素都找完还没有找到该数据为止,7.1.1用数组存储对象(续)为班级类添加查找方法,对象数组,已知学生的学号,查找此学生是否存在。

如果存在,返回其在数组中的下标位置;如果不存在,返回-1。

顺序查找方法的代码如下publicintfind(Stringid)for(inti=0;isize;i+)if(studentsi.getId().equals(id)returni;return-1;,7.1.1用数组存储对象(续)为班级类添加查找方法,对象数组,在数组的末尾增加一个学生对象增加之前需先判断数组中是否还有空间,并且在数组中查找将要增加的学号是否已经存在增加成功,返回true;否则,返回falsepublicbooleanadd(StudentaStudent)if(size=capacity)returnfalse;if(find(aStudent.getId()=0)returnfalse;this.studentssize=newStudent(newString(aStudent.getId(),newString(aStudent.getName(),aStudent.getEng(),aStudent.getMath(),aStudent.getComp();size+;returntrue;,7.1.1用数组存储对象(续)为班级类添加增加方法,对象数组,已知一个Student对象,将此对象从数组中删除publicbooleandel(StudentaStudent)intpos=find(aStudent.getId();if(pos=-1)returnfalse;for(inti=pos+1;isize;i+)studentsi-1=studentsi;size-;returntrue;,7.1.1用数组存储对象(续)为班级类编写删除方法,对象数组,已知学号,删除一个学生publicbooleandel(Stringid)intpos=find(id);if(pos=-1)returnfalse;for(inti=pos+1;isize;i+)studentsi-1=studentsi;size-;returntrue;,7.1.1用数组存储对象(续)为班级类添加删除方法,对象数组,7.1.2对数组元素进行排序,排序按照预先规定的准则(如升序或降序等),把数据有次序地排列起来已经设计出许多排序算法,常用的排序算法有选择排序插入排序交换排序以降序为例进行介绍,对象数组,选择排序的基本思想先在未排序序列中选一个最小元素,作为已排序子序列然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素直到把未排序子序列中的元素处理完为止,7.1.2对数组元素进行排序(续)选择排序,对象数组,用选择排序方法将例7-1中生成的文件stu.ser中的班级学生按总成绩从高到低排序在例7-1中的StudentClass类中增加选择排序方法selectionSort,代码如下publicvoidselectionSort()Studenttemp;for(inti=0;i0)temp=studentsi;studentsi=studentsj;studentsj=temp;,7.1.2对数组元素进行排序(续)例7_2,对象数组,测试类代码如下importjava.io.*;publicclassSortTesterpublicstaticvoidmain(Stringargs)Studentstudents=newStudent5;/从文件stu.ser中读出学生信息tryfi=new(stu.ser);ObjectInputStreamsi=newObjectInputStream(fi);for(inti=0;i5;i+)studentsi=(Student)si.readObject();si.close();catch(Exceptione)System.out.println(e);,7.1.2对数组元素进行排序(续)例7_2,对象数组,StudentClassaClass=newStudentClass(软件0201,5);aClass.setStudents(students);System.out.println(aClass);/选择排序aClass.selectionSort();System.out.println(选择排序后的结果:

n);System.out.println(aClass);,7.1.2对数组元素进行排序(续)例7_2,对象数组,运行结果班级:

软件0201容量:

40实际人数:

5学号姓名英语数学计算机总成绩250201李红887660224250202张林786780225250203董玉梅868075241250204张力706875213250205何为809078248选择排序后的结果:

班级:

软件0201容量:

40实际人数:

5学号姓名英语数学计算机总成绩250205何为809078248250203董玉梅868075241250202张林786780225250201李红887660224250204张力706875213,7.1.2对数组元素进行排序(续)例7_2运行结果,对象数组,插入排序将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止插入的规则不同,便形成了不同的插入排序方法。

其中,算法最简单的为直接插入排序方法直接插入排序方法先以未排序序列的第一个元素作为已排序子序列,然后从原来的第二个元素起,将各元素逐一插入到已排序子序列中合适的位置,直到把全部元素都插入为止。

7.1.2对数组元素进行排序(续)插入排序,对象数组,直接插入排序的步骤假设数组a中有n个元素a0、a1、an-1首先要把a0作为已排序子序列。

然后逐一将a1、a2、an-1插入到已排序子序列中。

每插入一个元素ai都要执行如下两步操作:

第一步,在已排序子序列中找一个合适位置j,使ajaiaj+1。

第二步,将ai插入到aj之后。

在插入之前,需要先保存ai的值,之后将aj+1、an-1依次向后移一位(后移操作也可在第一步的查找过程中进行),7.1.2对数组元素进行排序(续)直接插入排序,对象数组,在StudentClass中增加直接插入排序方法insertSort,代码如下publicvoidinsertSort()Studenttemp;for(inti=1;i-1,7.1.2对数组元素进行排序(续)例7_3,对象数组,7.1.3在已排序的数组中查找,一批Integer类型的数据已按升序排列好,a1a2an,存储在数组a0、a1、an-1中,现在要对该数组进行查找,看给定的数据x是否在此数组中顺序查找方法按从左向右的顺序查找,当x小于ai时就应该停止查找publicintseqSearch(intx)for(inti=0;(i=ai.intValue();i+)if(ai.intValue()=x)returni;return-1;二分查找。

在0到n-1中间选一个正整数k,用k把原来的有序序列分为三个有序子序列:

a0,a1,ak-1akak+1,ak+2,an-1,对象数组,具有排序数组的类SortedIntArraysearch方法运用二分查找算法:

在给定的数组范围内查找某一元素,如果存在,返回元素所在的下标位置,如果不存在,则返回元素应该在的位置(如果要将此元素插入到数组中,且保持数组仍然有序的位置)将此功能与插入功能相结合,可实现对数组元素进行排序publicclassSortedIntArrayprivateintcapacity;privateIntegerrep;privateintsize;publicSortedIntArray(intn)capacity=n;rep=newIntegercapacity;,7.1.3在已排序的数组中查找(续)例7_4,对象数组,publicSortedIntArray()this(100);privateintsearch(inti,intlower,intupper)intindex=lower;if(upper=lower)intmiddle=(upper+lower)/2;intcurrent=repmiddle.intValue();if(current=i)index=middle;elseif(currenti)index=search(i,middle+1,upper);elseindex=search(i,lower,middle-1);returnindex;,7.1.3在已排序的数组中查找(续)例7_4,对象数组,publicintsearch(inti)returnsearch(i,0,size-1);publicSortedIntArrayinsert(inti)Intindex=search(i);for(intj=size;jindex;-j)repj=repj-1;repindex=newInteger(i);+size;returnthis;publicSortedIntArrayremove(inti)intindex=search(i);if(repindex.intValue()=i)-size;for(intj=index;jsize;+j)repj=repj+1;returnthis;,7.1.3在已排序的数组中查找(续)例7_4,对象数组,publicStringtoString()StringtoReturn=;for(inti=0;isize;+i)toReturn+=repi.toString()+,;returntoReturn;staticpublicvoidmain(Stringargs)SortedIntArrayanArray=newSortedIntArray();anArray.insert(4).insert(9).insert(7).insert

(1).insert(3).insert

(2).insert(8).insert(7);System.out.println(anArray);anArray.remove

(1).remove(8).remove(7).remove(3);System.out.println(anArray);,7.1.3在已排序的数组中查找(续)例7_4,对象数组,运行结果1,2,3,4,7,7,8,9,2,4,7,9,7.1.3在已排序的数组中查找(续)例7_4运行结果,对象数组,7.3集合,数组的优点是Java提供的随机访问对象序列的最有效方法是一个简单的线性序列,访问元素的速度较快数组的缺点大小自创建以后就固定了,在其整个生存期内其大小不可改变数组元素只能是同一类型集合可动态改变其大小可在序列中存储不同类型的数据,集合把具有相同性质的一类东西,汇聚成一个整体在Java2中有很多与集合有关的接口及类它们被组织在以Collection及Map接口为根的层次结构中,称为集合框架在Java2之前,在Java1.0/1.1中,没有完整的集合框架。

只有一些简单的可以自扩展的容器类VectorHashtable,7.3集合(续),集合,7.3.1Java集合框架,集合框架(JavaCollectionsFramework)为表示和操作集合而规定的一种统一的标准的体系结构提供了一些现成的数据结构可供使用,程序员可以利用集合框架快速编写代码,并获得优良性能包含三大块内容对外的接口:

表示集合的抽象数据类型,使集合的操作与表示分开接口的实现:

指实现集合接口的Java类,是可重用的数据结构对集合运算的算法:

是指执行运算的方法,例如在集合上进行查找和排序,集合,集合框架接口声

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2