Java语言程序设计(郑莉)第七讲课后习题答案.docx
文本预览下载声明
Java语言程序设计第七章课后习题答案1.数组的声明与数组元素的创建有什么关系?答:声明数组仅仅是代表试图创建数组,不分配任何存储空间,声明是为创建做“铺垫”。2.Vector类的对象与数组有什么关系?什么时候适合使用数组,什么时候适合使用Vector?答:vector是一个能够存放任意对象类型的动态数组,容量能自动扩充,而数组存储固定且类型相同的对象;对于存储固定类型相同的对象使用数组,对于存储不同类型或者动态调整数组大小的情况使用Vector。3.与顺序查找相比,二分查找有什么优势?使用二分查找的条件?答:对于大数据量中进行查找时二分查找比顺序查找效率高得多;条件是已排序的数组。4.试举出三种常见的排序算法,并简单说明其排序思路。答:①选择排序:基本思想是站在未排序列中选一个最小元素,作为已排序子序列,然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素,直到把未排序列中的元素处理完为止。②插入排序:是将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止。③二分查找:将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。5.声明一个类People,成员变量有姓名、出生日期、性别、身高、体重等;生成10个People类对象,并放在一个以为数组中,编写方法按身高进行排序。//People类publicclass People{private String name;private String birthdaydate;private String sex;privatedoubleheight;privatedoubleweight;public People(){//默认构造函数}public People(People p){this.name=p.name;this.birthdaydate=p.birthdaydate;this.sex=p.sex;this.height=p.height;this.weight=p.weight;}public People(String name,String birthdaydate,String sex,double height,double weight){this.name=name;this.birthdaydate=birthdaydate;this.sex=sex;this.height=height;this.weight=weight;}public String getName() {returnname;}publicvoid setName(String name) {this.name = name;}public String getBirthdaydate() {returnbirthdaydate;}publicvoid setBirthdaydate(String birthdaydate) {this.birthdaydate = birthdaydate;}public String getSex() {returnsex;}publicvoid setSex(String sex) {this.sex = sex;}publicdouble getHeight() {returnheight;}publicvoid setHeight(double height) {this.height = height;}publicdouble getWeight() {returnweight;}publicvoid setWeight(double weight) {this.weight = weight;}public String toString(){return姓名:+name+\n出生年月:+birthdaydate+\n性别:+sex+\n身高:+height+\n体重:+weight;}}//test7_5类publicclass test7_5 {/***@paramargs*/publicstaticvoid main(String[] args) {// TODO Auto-generated method stubPeople[] people={new People(林楚金,1989年8月13日,男,182,63.5),new People(诸葛亮,181年7月23日,男,184,76.6),new Peop
显示全部