数据结构与算法分析(第3版)练习题-第1章-引论

时间:2023-02-23 19:52:05

入行Java不久,仅个人练习Java语言所记录,不足之处,请多指点。


1.13 设计一个泛型类Collection,它存储Object对象的集合(在数组中),以及该集合的当前大小。提供public 方法isEmpty,makeEmpty,insert,remove和isPresent。方法isPresent(X)当且仅当在该集合中存在(由equals定义)等于x的一个Object时返回true。


package org.my.excreise;

public class Excrise {

	public static class Collection {
		private Object[] objects;

		public Object[] getObjects() {
			return objects;
		}

		public void setObjects(Object[] objects) {
			this.objects = objects;
		}

		public boolean isEmpty() {
			if (objects.length == 0 || objects == null) {
				return true;
			} else {
				return false;
			}
		}

		public void makeEmpty() {
			objects = null;
		}

		public void insert(Object object) {
			int length = this.objects.length;
			Object[] objects = new Object[length + 1];
			System.arraycopy(this.objects, 0, objects, 0, length);// 复制元素
			objects[length] = object;
			this.objects=objects;
		}

		public void remove(int index) {
			if (index < 0 || this.objects == null || this.objects.length == 0) {
				return;
			}
			int length = this.objects.length;
			if (index > length - 1) {
				return;
			}
			if (length == 1) {
				this.objects = new Object[] {};
			}
			Object[] objects = new Object[length - 1];
			for (int i = 0; i < length; i++) {
				if (i == index) {
					continue;
				}
				if (i < index) {
					objects[i] = this.objects[i];
				} else {
					objects[i - 1] = this.objects[i];
				}
			}
			this.objects = objects;
		}
		
		public boolean isPresent(Object xObject) {
			if (objects.length==0 || objects==null) {
				return false;				
			}
			for (Object object : objects) {
				if (object.equals(xObject)) {
					return true;
				}
			}
			return false;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Collection collection=new Collection();
        collection.setObjects(new Object[]{1,"12.5","我的博客"});
        for (Object object : collection.getObjects()) {
			System.out.println(object);
		}
        System.out.println("--------------------------");
        collection.remove(1);
        for (Object object : collection.getObjects()) {
			System.out.println(object);
		}
        System.out.println("--------------------------");
        collection.insert("csdn");
        for (Object object : collection.getObjects()) {
			System.out.println(object);
		}
	}
}

1.14 设计一个泛型类OrderdCollection,它存储Comparable的对象的集合(在数组中),以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和findMax。findMin和findMax分别返回该集合中最小的和最大的Comparable对象的引用(如果该集合为空,则返回null)


package org.my.excreise;

public class Excrise {

	public static class OrderedCollection {
		private Comparable[] comparables;

		public Comparable[] getComparables() {
			return comparables;
		}

		public void setComparables(Comparable[] comparables) {
			this.comparables = comparables;
		}
		public boolean isEmpty() {
			if (comparables.length==0 || comparables==null) {
				return true;
			}
			else {
				return false;
			}
		}
		public void makeEmpty() {
			comparables=new Comparable[]{};
		}
		public void insert(Comparable comparable) {
			int length=this.comparables.length;
			Comparable[] comparables=new Comparable[length+1];
			System.arraycopy(this.comparables, 0, comparables, 0, length);
			comparables[length]=comparable;
			this.comparables=comparables;
		}
		public void remove(int index) {
			if (index < 0 || this.comparables == null || this.comparables.length == 0) {
				return;
			}
			int length = this.comparables.length;
			if (index > length - 1) {
				return;
			}
			if (length == 1) {
				this.comparables = new Comparable[] {};
			}
			Comparable[] objects = new Comparable[length - 1];
			for (int i = 0; i < length; i++) {
				if (i == index) {
					continue;
				}
				if (i < index) {
					objects[i] = this.comparables[i];
				} else {
					objects[i - 1] = this.comparables[i];
				}
			}
			this.comparables = objects;
		}
		
		public Comparable findMax() {
			if (comparables.length==0 || comparables==null) {
				return null;
			}
			int maxindex=0;
			for (int i = 0; i < comparables.length; i++) {
				if (comparables[i].compareTo(comparables[maxindex])>0) {
					maxindex=i;
				}
			}
			return comparables[maxindex];
		}
		public Comparable findMin() {
			if (comparables.length==0 || comparables==null) {
				return null;
			}
			int maxindex=0;
			for (int i = 0; i < comparables.length; i++) {
				if (comparables[i].compareTo(comparables[maxindex])<0) {
					maxindex=i;
				}
			}
			return comparables[maxindex];
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Comparable[] comparables=new Comparable[]{1,3,5,6,9};
		OrderedCollection collection=new OrderedCollection();
		collection.setComparables(comparables);
		Comparable comparable=collection.findMax();
		System.out.println(comparable);
	}
}



1.15 定义一个Rectangle类,该类提供getLength和getWidth方法。利用图1-18中的findMax例程编写一种main方法,该方法创建一个Rectangle数组并首先找出面积最大的Rectangle对象,然后找出周长最大的Rectangle对象。


package org.my.excreise;

import java.math.BigDecimal;


public class Excrise {

	public interface IMyCompare<T>
	{
		int CompareArea(T t1,T t2);
		int CompareCirc(T t1,T t2);
	}
	
	public static class Rectangle
	{
		private float length=0f;
		private float width=0f;
		public float getLength() {
			return length;
		}
		public void setLength(float length) {
			this.length = length;
		}
		public float getWidth() {
			return width;
		}
		public void setWidth(float width) {
			this.width = width;
		}		
		
		public float getArea()
		{
			BigDecimal b1=new BigDecimal(Float.toString(length));
			BigDecimal b2=new BigDecimal(Float.toString(width));
			float area=b1.multiply(b2).floatValue();
			return area;
			
		}
		
		public float getCirc()
		{
			BigDecimal b1=new BigDecimal(Float.toString(length));
			BigDecimal b2=new BigDecimal(Float.toString(width));
			float circ=((b1.add(b2)).multiply(new BigDecimal(Float.toString(2.0f)))).floatValue();
			return circ;
		}
	}
	
	public static class Compare implements IMyCompare<Rectangle>
	{

		@Override
		public int CompareArea(Rectangle t1, Rectangle t2) {
			// TODO Auto-generated method stub
			if (t1.getArea()>t2.getArea()) {
				return 1;
			}
			else {
				return 0;
			}
		}

		@Override
		public int CompareCirc(Rectangle t1, Rectangle t2) {
			// TODO Auto-generated method stub
			if (t1.getCirc()>t2.getCirc()) {
				return 1;
			}
			else {
				return 0;
			}
		}		
	}
	
    public static <T> int[] findMax(T[] arr,IMyCompare<? super T> myCompare) {
		int max1=0,max2=0;
		for (int i = 0; i < arr.length; i++) {
			if (myCompare.CompareArea(arr[i], arr[max1])==1) {
				max1=i;
			}
			if (myCompare.CompareCirc(arr[i], arr[max2])==1) {
			    max2=i;	
			}
		}
		return new int[]{max1,max2};		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Rectangle[] aRectangles={new Rectangle(),new Rectangle(),new Rectangle()};
        aRectangles[0].length=3.1f;
        aRectangles[0].width=3.1f;
        aRectangles[1].length=1.2f;
        aRectangles[1].width=1.2f;
        aRectangles[2].length=1.3f;
        aRectangles[2].width=1.3f;
        int[] arr=findMax(aRectangles, new Compare());
        System.out.println("最大面积="+aRectangles[arr[0]].getArea()+",最大周长="+aRectangles[arr[1]].getCirc());
	}
}