Java语言程序设计郑莉第七章课后习题答案.docx
- 文档编号:2916374
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:14
- 大小:85.55KB
Java语言程序设计郑莉第七章课后习题答案.docx
《Java语言程序设计郑莉第七章课后习题答案.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计郑莉第七章课后习题答案.docx(14页珍藏版)》请在冰豆网上搜索。
Java语言程序设计郑莉第七章课后习题答案
Java语言程序设计
第七章课后习题答案
1.数组的声明与数组元素的创建有什么关系?
答:
声明数组仅仅是代表试图创建数组,不分配任何存储空间,声明是为创建做“铺垫”。
2.Vector类的对象与数组有什么关系?
什么时候适合使用数组,什么时候适合使用Vector?
答:
vector是一个能够存放任意对象类型的动态数组,容量能自动扩充,而数组存储固定且类型相同的对象;对于存储固定类型相同的对象使用数组,对于存储不同类型或者动态调整数组大小的情况使用Vector。
3.与顺序查找相比,二分查找有什么优势?
使用二分查找的条件?
答:
对于大数据量中进行查找时二分查找比顺序查找效率高得多;条件是已排序的数组。
4.试举出三种常见的排序算法,并简单说明其排序思路。
答:
①选择排序:
基本思想是站在未排序列中选一个最小元素,作为已排序子序列,然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素,直到把未排序列中的元素处理完为止。
②插入排序:
是将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止。
③二分查找:
将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。
重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
5.声明一个类People,成员变量有姓名、出生日期、性别、身高、体重等;生成10个People类对象,并放在一个以为数组中,编写方法按身高进行排序。
//People类
publicclassPeople{
privateStringname;
privateStringbirthdaydate;
privateStringsex;
privatedoubleheight;
privatedoubleweight;
publicPeople(){//默认构造函数
}
publicPeople(Peoplep){
this.name=p.name;
this.birthdaydate=p.birthdaydate;
this.sex=p.sex;
this.height=p.height;
this.weight=p.weight;
}
publicPeople(Stringname,Stringbirthdaydate,Stringsex,doubleheight,doubleweight){
this.name=name;
this.birthdaydate=birthdaydate;
this.sex=sex;
this.height=height;
this.weight=weight;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetBirthdaydate(){
returnbirthdaydate;
}
publicvoidsetBirthdaydate(Stringbirthdaydate){
this.birthdaydate=birthdaydate;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicdoublegetWeight(){
returnweight;
}
publicvoidsetWeight(doubleweight){
this.weight=weight;
}
publicStringtoString(){
return"姓名:
"+name+"\n出生年月:
"+birthdaydate+"\n性别:
"+sex+"\n身高:
"+height+"\n体重:
"+weight;
}
}
//test7_5类
publicclasstest7_5{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
People[]people={
newPeople("林楚金","1989年8月13日","男",182,63.5),
newPeople("诸葛亮","181年7月23日","男",184,76.6),
newPeople("迈克杰克逊","1958年8月29日","男",180,60),
newPeople("乔丹","1963年2月17日","男",198,98.1),
newPeople("拿破仑","1769年8月15日","男",159.5,63),
newPeople("苍井空","1983年11月11日","女",155,45),};
Peopletemp=newPeople();
for(inti=0;i for(intj=i+1;j if(people[i].getHeight() temp=people[j]; people[j]=people[i]; people[i]=temp; } } System.out.println("按身高从小到大排序后的结果如下: "); for(inti=0;i System.out.println(people[i]+"\n"); } } 运行结果: 6.声明一个类,此类使用私有的ArrayList来存储对象。 使用一个Class类的引用得到第一个对象的类型之后,只允许用户插入这种类型的对象。 //Fuck类 importjava.util.ArrayList; publicclassFuck{ privateArrayListman=newArrayList(); privateClassclassType=null; publicvoidadd(Objectf){ if(man.size()==0){ classType=f.getClass(); } if(classType.equals(f.getClass())){ man.add(f); System.out.println("插入成功."); } else{ System.out.println("只允许插入"+getClassType()+"类的对象."); } } publicArrayListgetMan(){ returnman; } publicClassgetClassType(){ returnclassType; } publicFuck(){} } //test7_6 publicclasstest7_6{ publicstaticvoidmain(String[]args){ Fuckfuckman=newFuck(); Strings=newString("林楚金"); fuckman.add(s); fuckman.add(10);//测试插入插入整数 fuckman.add('f');//测试插入插入字符 fuckman.add("希特勒"); System.out.println(fuckman.getMan()); } } 运行结果: 7.找出一个二维数组的鞍点,即该位置上的元素在所在行上最大,在所在列上最小。 (也可能没有鞍点) //test7_7 importjava.util.Scanner; publicclasstest7_7{ publicstaticvoidmain(String[]args){ introw,series,max; booleanT=false; Scannercin=newScanner(System.in); System.out.println("请输入数组的行数"); row=cin.nextInt(); System.out.println("请输入数组的列数"); series=cin.nextInt(); int[][]Array=newint[row][series]; int[]R=newint[row];//记录每行最大的数的列标 int[]S=newint[series];//记录每列最小的数的行标 System.out.println("请输入数组内容"); for(inti=0;i for(intj=0;j {Array[i][j]=cin.nextInt(); if(j==series-1){ max=Array[i][0]; for(intz=1;z if(Array[i][z]>max) { max=Array[i][z]; R[i]=z; } } } for(intj=0;j { max=Array[0][j]; for(intz=1;z if(Array[z][j] { max=Array[z][j]; S[j]=z; } } for(inti=0;i { if(S[R[i]]==i) { System.out.println("鞍点: "+"Array["+i+"]["+R[i]+ "]: "+Array[i][R[i]]+"\n"); T=true; } } if(T==false) System.out.println("没有鞍点"); } } 运行结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言程序设计 第七 课后 习题 答案