[VIP专享]Java语言程序设计(郑莉)第七章课后习题答案

  • 格式:pdf
  • 大小:296.64 KB
  • 文档页数:11

Java语言程序设计第七章课后习题答案

1.数组的声明与数组元素的创建有什么关系?答:声明数组仅仅是代表试图创建数组,不分配任何存储空间,声明是为创建做“铺垫”。

2.Vector类的对象与数组有什么关系?什么时候适合使用数组,什么时候适合使用Vector?答:vector是一个能够存放任意对象类型的动态数组,容量能自动扩充,而数组存储固定且类型相同的对象;对于存储固定类型相同的对象使用数组,对于存储不同类型或者动态调整数组大小的情况使用Vector。

3.与顺序查找相比,二分查找有什么优势?使用二分查找的条件?答:对于大数据量中进行查找时二分查找比顺序查找效率高得多;条件是已排序的数组。

4.试举出三种常见的排序算法,并简单说明其排序思路。答:①选择排序:基本思想是站在未排序列中选一个最小元素,作为已排序子序列,然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素,直到把未排序列中的元素处理完为止。②插入排序:是将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止。③二分查找:将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。

5.声明一个类People,成员变量有姓名、出生日期、性别、身高、体重等;生成10个People类对象,并放在一个以为数组中,编写方法按身高进行排序。

//People类public class People{private String name;private String birthdaydate;private String sex;private double height;private double weight;

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() {return name;}

public void setName(String name) {this.name = name;}

public String getBirthdaydate() {return birthdaydate;}

public void setBirthdaydate(String birthdaydate) {this.birthdaydate = birthdaydate;}

public String getSex() {return sex;}

public void setSex(String sex) {this.sex = sex;}

public double getHeight() {return height;}

public void setHeight(double height) {this.height = height;}

public double getWeight() {return weight;}

public void setWeight(double weight) {this.weight = weight;}

public String toString(){return "姓名:"+name+"\n出生年月:"+birthdaydate+"\n性别:"+sex+"\n身高:"+height+"\n体重:"+weight;}}

//test7_5类public class test7_5 {/** * @param args */public static void 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 People("迈克杰克逊","1958年8月29日","男",180,60),new People("乔丹","1963年2月17日","男",198,98.1),new People("拿破仑","1769年8月15日","男",159.5,63),new People("苍井空","1983年11月11日","女",155,45),};

People temp=new People();for(int i=0;ifor(int j=i+1;jif(people[i].getHeight()temp=people[j];people[j]=people[i];people[i]=temp;}}System.out.println("按身高从小到大排序后的结果如下:");for(int i=0;iSystem.out.println(people[i]+"\n");

}}运行结果:6.声明一个类,此类使用私有的ArrayList来存储对象。使用一个Class类的引用得到第一个对象的类型之后,只允许用户插入这种类型的对象。

// Fuck类

import java.util.ArrayList;

public class Fuck {private ArrayList man=new ArrayList();private Class classType=null; public void add(Object f){ if(man.size()==0){ classType=f.getClass(); } if(classType.equals(f.getClass())){ man.add(f); System.out.println("插入成功."); } else{ System.out.println("只允许插入"+getClassType()+"类的对象."); } }public ArrayList getMan() {return man;}public Class getClassType() {return classType;}public Fuck(){}}

//test7_6public class test7_6 {public static void main(String[] args) {

Fuck fuckman=new Fuck();String s=new String("林楚金");

fuckman.add(s);fuckman.add(10);//测试插入插入整数fuckman.add('f');//测试插入插入字符fuckman.add("希特勒");

System.out.println(fuckman.getMan());}}运行结果:

7.找出一个二维数组的鞍点,即该位置上的元素在所在行上最大,在所在列上最小。(也可能没有鞍点)//test7_7

import java.util.Scanner;public class test7_7 {public static void main(String[] args) { int row, series, max; boolean T=false; Scanner cin = new Scanner(System.in); System.out.println("请输入数组的行数"); row = cin.nextInt(); System.out.println("请输入数组的列数"); series = cin.nextInt(); int[][] Array = new int[row][series]; int[] R = new int[row];// 记录每行最大的数的列标 int[] S = new int[series];// 记录每列最小的数的行标 System.out.println("请输入数组内容"); for (int i = 0; i < Array.length; i++) for (int j = 0; j < Array[i].length; j++) { Array[i][j] = cin.nextInt(); if(j==series-1) { max = Array[i][0]; for (int z = 1; z < series; z++) if (Array[i][z] > max) { max = Array[i][z]; R[i] = z; } }