好文档 - 专业文书写作范文服务资料分享网站

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

天下 分享 时间: 加入收藏 我要投稿 点赞

《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]; } }

8cjjv83klq9ersa9pruq6ksx797jw500ws8
领取福利

微信扫码领取福利

微信扫码分享