Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Iterator;
- public class MyContainer <T extends Learner> extends ArrayList<T> implements Cloneable{
- public T min() throws EmptyContainerException{
- if(this.isEmpty()){
- throw new EmptyContainerException();
- }
- // T minElem=this.get(0);
- // //1
- // for(Iterator<T> it = this.iterator(); it.hasNext(); ){
- // T elem = it.next();
- // if(elem.compareTo(minElem)<0){
- // minElem=elem;
- // }
- // }
- // //2
- //// for(T elem : this ){
- //// if(elem.compareTo(minElem)<0){
- //// minElem=elem;
- //// }
- //// }
- // //3
- return Collections.min(this) ;
- }
- public void printContent() throws EmptyContainerException{
- if(this.isEmpty()){
- throw new EmptyContainerException();
- }
- for(T elem : this ){
- System.out.println(elem.toString());
- }
- }
- public Learner findElem(Learner learner) throws EmptyContainerException{
- if(this.isEmpty()){
- throw new EmptyContainerException();
- }
- MyContainer<Learner> myContainer = (MyContainer<Learner>) this.clone();
- Collections.sort(myContainer);
- int index = Collections.binarySearch(myContainer, learner);
- if(index>=0) {
- return myContainer.get(index);
- }
- else{
- return null;
- }
- }
- public int countSameElem(Learner learner) throws EmptyContainerException{
- if(this.isEmpty()){
- throw new EmptyContainerException();
- }
- // int counter = 0 ;
- // for(T elem : this){
- // if(elem.compareTo(learner)==0){
- // counter++;
- // }
- // }
- // return counter;
- return Collections.frequency(this,learner);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment