YauhenMardan

MyContainer

Dec 5th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. public class MyContainer <T extends Learner> extends ArrayList<T> implements Cloneable{
  7. public T min() throws EmptyContainerException{
  8. if(this.isEmpty()){
  9. throw new EmptyContainerException();
  10. }
  11. return Collections.min(this) ;
  12. }
  13. public void printContent() throws EmptyContainerException{
  14. if(this.isEmpty()){
  15. throw new EmptyContainerException();
  16. }
  17. for(T elem : this ){
  18. elem.printInfo();
  19. }
  20. }
  21. public Learner findElem(T t) throws EmptyContainerException{
  22. if(this.isEmpty()){
  23. throw new EmptyContainerException();
  24. }
  25. MyContainer<T> myContainer = (MyContainer<T>) this.clone();
  26. Collections.sort(myContainer);
  27. int index = Collections.binarySearch(myContainer, t);
  28. if(index>=0) {
  29. return myContainer.get(index);
  30. }
  31. else{
  32. return null;
  33. }
  34. }
  35. public int countSameElem(T t) throws EmptyContainerException{
  36. if(this.isEmpty()){
  37. throw new EmptyContainerException();
  38. }
  39. return Collections.frequency(this,t);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment