YauhenMardan

Untitled

Dec 4th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Iterator;
  7.  
  8. public class MyContainer <T extends Learner> extends ArrayList<T> implements Cloneable{
  9. public T min() throws EmptyContainerException{
  10. if(this.isEmpty()){
  11. throw new EmptyContainerException();
  12. }
  13. // T minElem=this.get(0);
  14. // //1
  15. // for(Iterator<T> it = this.iterator(); it.hasNext(); ){
  16. // T elem = it.next();
  17. // if(elem.compareTo(minElem)<0){
  18. // minElem=elem;
  19. // }
  20. // }
  21. // //2
  22. //// for(T elem : this ){
  23. //// if(elem.compareTo(minElem)<0){
  24. //// minElem=elem;
  25. //// }
  26. //// }
  27. // //3
  28. return Collections.min(this) ;
  29. }
  30. public void printContent() throws EmptyContainerException{
  31. if(this.isEmpty()){
  32. throw new EmptyContainerException();
  33. }
  34. for(T elem : this ){
  35. System.out.println(elem.toString());
  36. }
  37. }
  38. public Learner findElem(Learner learner) throws EmptyContainerException{
  39. if(this.isEmpty()){
  40. throw new EmptyContainerException();
  41. }
  42. MyContainer<Learner> myContainer = (MyContainer<Learner>) this.clone();
  43. Collections.sort(myContainer);
  44. int index = Collections.binarySearch(myContainer, learner);
  45. if(index>=0) {
  46. return myContainer.get(index);
  47. }
  48. else{
  49. return null;
  50. }
  51. }
  52. public int countSameElem(Learner learner) throws EmptyContainerException{
  53. if(this.isEmpty()){
  54. throw new EmptyContainerException();
  55. }
  56. // int counter = 0 ;
  57. // for(T elem : this){
  58. // if(elem.compareTo(learner)==0){
  59. // counter++;
  60. // }
  61. // }
  62. // return counter;
  63. return Collections.frequency(this,learner);
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment