1.14 设计一个泛型类orderedCollection

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

import java.util.Arrays; /** * * @author 疯狂龅牙酥 * */ public class One_forteen {  public static void main(String[] args) {  // TODO Auto-generated method stub Comparable[] c = new Comparable[] { 1,2,3,4}; OrderedCollection arr = new OrderedCollection(c); System.out.println(arr.findMax()); System.out.println(arr.findMin()); System.out.println(Arrays.toString(arr.getOrderedCollection())); System.out.println(arr.isEmpty()); arr.makeEmpty(); System.out.println(arr.isEmpty()); arr.insert(8); arr.insert(9); arr.insert(10); System.out.println(arr.isEmpty()); System.out.println(Arrays.toString(arr.getOrderedCollection())); arr.remove(1); System.out.println(arr.isEmpty()); System.out.println(Arrays.toString(arr.getOrderedCollection())); System.out.println(arr.findMax()); System.out.println(arr.findMin()); arr.remove(0); arr.remove(0); System.out.println(arr.isEmpty()); System.out.println(Arrays.toString(arr.getOrderedCollection())); System.out.println(arr.findMin()); } } class OrderedCollection{  private Comparable[] obj; private int length = 0; public OrderedCollection(Comparable[] obj) {  this.obj = obj; this.length = obj.length; } public Comparable[] getOrderedCollection() {  return this.obj; } public boolean isEmpty() {  return this.length == 0; } public void makeEmpty() {  this.obj = new Comparable[] { }; this.length = 0; } public void insert(Comparable c) {  Comparable[] tmp = new Comparable[this.length+1]; System.arraycopy(this.obj, 0, tmp, 0, this.length++); tmp[this.length-1] = c; this.obj = tmp; } public void remove(int index) {  Comparable[] tmp = new Comparable[this.length-1]; System.arraycopy(this.obj, 0, tmp, 0, index); System.arraycopy(this.obj, index + 1, tmp, index, --this.length-index); this.obj = tmp; } public Comparable findMax() {  if(this.obj == null || this.length==0) {  return null; }else {  Comparable max = this.obj[0]; for(int i=1; i<this.length; i++) {  if(max.compareTo(this.obj[i])<0) {  max = this.obj[i]; } } return max; } } public Comparable findMin() {  if(this.obj == null || this.length==0) {  return null; }else {  Comparable min = this.obj[0]; for(int i=1; i<this.length; i++) {  if(min.compareTo(this.obj[i])>0) {  min = this.obj[i]; } } return min; } } } 
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务