Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package aufgabe2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class CharCollection {
  7.     private List<Character> chars = new ArrayList<>();
  8.  
  9.     CharCollection(CharCollection cc){
  10.         List<Character> chars = new ArrayList<>();
  11.        
  12.         for(int i = 0; i < cc.size(); i++) {
  13.             chars.add(cc.getChars().get(i));
  14.         }
  15.        
  16.         setChars(chars);
  17.     }
  18.    
  19.     CharCollection(char... cs) {
  20.         List<Character> chars = new ArrayList<>();
  21.        
  22.         for(int i = 0; i < cs.length; i++) {
  23.             chars.add(cs[i]);
  24.         }
  25.        
  26.         setChars(chars);
  27.     }
  28.    
  29.     CharCollection(String s){
  30.         List<Character> chars = new ArrayList<>();
  31.        
  32.         for(int i = 0; i < s.length(); i++) {
  33.             chars.add(s.charAt(i));
  34.         }
  35.        
  36.         setChars(chars);
  37.     }
  38.    
  39.     public int size() {
  40.         return chars.size();
  41.     }
  42.    
  43.     public int count(char c) {
  44.         Character c2 = c;
  45.         int count = 0;
  46.        
  47.         for(int i = 0; i < size(); i++) {
  48.             if(getChars().get(i).equals(c2)) {
  49.                 count++;
  50.             }
  51.         }
  52.        
  53.         return count;
  54.     }
  55.    
  56.     public int different() {
  57.         List<Character> tmp = new ArrayList<>();
  58.        
  59.         for(int i = 0; i < size(); i++) {
  60.             if(!tmp.contains(chars.get(i))) {
  61.                 tmp.add(chars.get(i));
  62.             }
  63.         }
  64.        
  65.         return tmp.size();
  66.     }
  67.    
  68.     public char top() {
  69.         if(size() == 0) {
  70.             return 0;
  71.         }
  72.        
  73.         int topCharIndex = 0;
  74.         int topCharAmount = 0;
  75.        
  76.         for(int i = 0; i < size(); i++) {
  77.             if(count(chars.get(i)) > topCharAmount) {
  78.                 topCharAmount = count(chars.get(i));
  79.                 topCharIndex = i;
  80.             }
  81.         }
  82.        
  83.         return chars.get(topCharIndex);
  84.     }
  85.    
  86.     public String toString() {
  87.         String s = "(";
  88.        
  89.         for(int i = 0; i < size(); i++) {
  90.             s += chars.get(i);
  91.             if(i < size()-1) {
  92.                 s += ", ";
  93.             }
  94.         }
  95.        
  96.         s += ")";
  97.         return s;
  98.     }
  99.    
  100.     public CharCollection moreThan(int m) {
  101.         CharCollection returnCollection = new CharCollection("");
  102.        
  103.         for(int i = 0; i < size(); i++) {
  104.             if(count(this.chars.get(i)) > m) {
  105.                 returnCollection.getChars().add(this.getChars().get(i));
  106.             }
  107.         }
  108.        
  109.         return returnCollection;
  110.     }
  111.    
  112.     public boolean equals(Object x) {
  113.         if(x instanceof CharCollection) {
  114.             for(int i = 0; i < size(); i++) {
  115.                 Character tmp = this.getChars().get(i);
  116.                 if(((CharCollection) x).count(tmp) != this.count(tmp)) {
  117.                     return false;
  118.                 }
  119.             }
  120.             return true;
  121.         } else {
  122.             return false;
  123.         }
  124.     }
  125.    
  126.     public CharCollection except(CharCollection cc) {
  127.         CharCollection ccReturn = new CharCollection(this);
  128.        
  129.         for(int i = 0; i < cc.size(); i++) {
  130.             char tmp = cc.getChars().get(i);
  131.             for(int j = 0; j < ccReturn.size(); j++) {
  132.                 if(tmp == ccReturn.getChars().get(j)) {
  133.                     ccReturn.getChars().remove(j);
  134.                     break;
  135.                 }
  136.             }
  137.         }
  138.        
  139.         return ccReturn;
  140.     }
  141.    
  142.     public boolean isSubset(CharCollection cc) {
  143.         for(int i = 0; i < cc.size(); i++) {
  144.             if(cc.count(cc.getChars().get(i)) >  this.count(cc.getChars().get(i))) {
  145.                 return false;
  146.             }
  147.         }
  148.         return true;
  149.     }
  150.  
  151.     public List<Character> getChars() {
  152.         return chars;
  153.     }
  154.  
  155.    
  156.     public void setChars(List<Character> chars) {
  157.         this.chars = chars;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement