第一范文网 - 专业文章范例文档资料分享平台

《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十九章练习题答案

来源:用户分享 时间:2025/6/15 0:26:36 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

《Java语言程序设计(基础篇)》(第10版 梁勇 著)

第十九章 练习题答案

19.1

class GenericStack {

public final static int INITIAL_SIZE = 16; private E[] elements; private int size;

/** Construct a stack with the default initial capacity */ public GenericStack() { this(INITIAL_SIZE); }

/** Construct a stack with the specified initial capacity */ public GenericStack(int initialCapacity) { elements = (E[])new Object[initialCapacity]; }

/** Push a new element into the top of the stack */ public E push(E value) {

if (size >= elements.length) {

E[] temp = (E[])new Object[elements.length * 2];

System.arraycopy(elements, 0, temp, 0, elements.length); elements = temp; }

return elements[size++] = value; }

/** Return and remove the top element from the stack */ public E pop() {

return elements[--size]; }

/** Return the top element from the stack */ public E peek() {

return elements[size - 1]; }

/** Exercise03_21 whether the stack is empty */ public boolean isEmpty() {

return size == 0; }

/** Return the number of elements in the stack */ public int getSize() { return size; } }

19.2

public class Exercise19_02 {

public static void main(String[] args) {

GenericStack stack = new GenericStack(); stack.push(\); stack.push(\); stack.push(\);

System.out.println(stack.getSize()); System.out.println(stack.peek()); System.out.println(stack.pop()); System.out.println(stack.peek()); }

// GenericStack.java: Implementing a stack using inheritance static class GenericStack extends java.util.ArrayList { public boolean isEmpty() { return super.isEmpty(); }

public int getSize() { return size(); }

public Object peek() { return get(getSize() - 1); }

public Object pop() {

Object o = get(getSize() - 1); remove(getSize() - 1); return o; }

public Object push(E o) {

add(o); return o; }

public int search(Object o) { return indexOf(o); }

@Override

public String toString() { return \ + toString(); } } }

19.3

import java.util.ArrayList;

public class Exercise19_03 {

public static void main(String[] args) {

ArrayList list = new ArrayList(); list.add(14); list.add(24); list.add(14); list.add(42); list.add(25);

ArrayList newList = removeDuplicates(list);

System.out.print(newList); }

public static ArrayList removeDuplicates(ArrayList list) { ArrayList result = new ArrayList();

for (E e: list) {

if (!result.contains(e)) result.add(e); }

return result; } }

19.4

public class Exercise19_04 {

public static > void selectionSort(E[] list) { for (int i = 1; i < list.length; i++) {

/** insert list[i] into a sorted sublist list[0..i-1] so that list[0..i] is sorted. */ E currentElement = list[i]; int k;

for (k = i - 1; k >= 0 && list[k].compareTo(currentElement) > 0; k--) { list[k + 1] = list[k]; }

// Insert the current element into list[k+1] list[k + 1] = currentElement; } } }

19.5

public class Exercise19_05 {

public static void main(String[] args) { Integer[] numbers = {1, 2, 3}; System.out.println(max(numbers));

String[] words = {\, \, \}; System.out.println(max(words));

Circle[] circles = {new Circle(3), new Circle(2.9), new Circle(5.9)}; System.out.println(max(circles)); }

static class Circle implements Comparable { double radius;

public Circle(double radius) { this.radius = radius; }

@Override

public int compareTo(Circle c) { if (radius < c.radius)

return -1;

else if (radius == c.radius) return 0; else return 1; }

@Override

public String toString() {

return \ + radius; } }

public static > E max(E[] list) { E max = list[0];

for (int i = 1; i < list.length; i++) { if (max.compareTo(list[i]) < 0) { max = list[i]; } }

return max; } }

19.6

public class Exercise19_06 {

public static void main(String[] args) { Integer[][] numbers = { {1, 2, 3}, {4, 4, 6} };

System.out.println(max(numbers)); }

public static> E max(E[][] list) { E max = list[0][0];

for (int i = 1; i < list.length; i++) { for (int j = 1; j < list[i].length; j++) { if (max.compareTo(list[i][j]) < 0) { max = list[i][j]; } }

《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十九章练习题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8cjjv83klq9ersa9pruq6ksx797jw500ws8_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top