Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aufgabe2;
- import java.util.ArrayList;
- import java.util.List;
- public class CharCollection {
- private List<Character> chars = new ArrayList<>();
- CharCollection(CharCollection cc){
- List<Character> chars = new ArrayList<>();
- for(int i = 0; i < cc.size(); i++) {
- chars.add(cc.getChars().get(i));
- }
- setChars(chars);
- }
- CharCollection(char... cs) {
- List<Character> chars = new ArrayList<>();
- for(int i = 0; i < cs.length; i++) {
- chars.add(cs[i]);
- }
- setChars(chars);
- }
- CharCollection(String s){
- List<Character> chars = new ArrayList<>();
- for(int i = 0; i < s.length(); i++) {
- chars.add(s.charAt(i));
- }
- setChars(chars);
- }
- public int size() {
- return chars.size();
- }
- public int count(char c) {
- Character c2 = c;
- int count = 0;
- for(int i = 0; i < size(); i++) {
- if(getChars().get(i).equals(c2)) {
- count++;
- }
- }
- return count;
- }
- public int different() {
- List<Character> tmp = new ArrayList<>();
- for(int i = 0; i < size(); i++) {
- if(!tmp.contains(chars.get(i))) {
- tmp.add(chars.get(i));
- }
- }
- return tmp.size();
- }
- public char top() {
- if(size() == 0) {
- return 0;
- }
- int topCharIndex = 0;
- int topCharAmount = 0;
- for(int i = 0; i < size(); i++) {
- if(count(chars.get(i)) > topCharAmount) {
- topCharAmount = count(chars.get(i));
- topCharIndex = i;
- }
- }
- return chars.get(topCharIndex);
- }
- public String toString() {
- String s = "(";
- for(int i = 0; i < size(); i++) {
- s += chars.get(i);
- if(i < size()-1) {
- s += ", ";
- }
- }
- s += ")";
- return s;
- }
- public CharCollection moreThan(int m) {
- CharCollection returnCollection = new CharCollection("");
- for(int i = 0; i < size(); i++) {
- if(count(this.chars.get(i)) > m) {
- returnCollection.getChars().add(this.getChars().get(i));
- }
- }
- return returnCollection;
- }
- public boolean equals(Object x) {
- if(x instanceof CharCollection) {
- for(int i = 0; i < size(); i++) {
- Character tmp = this.getChars().get(i);
- if(((CharCollection) x).count(tmp) != this.count(tmp)) {
- return false;
- }
- }
- return true;
- } else {
- return false;
- }
- }
- public CharCollection except(CharCollection cc) {
- CharCollection ccReturn = new CharCollection(this);
- for(int i = 0; i < cc.size(); i++) {
- char tmp = cc.getChars().get(i);
- for(int j = 0; j < ccReturn.size(); j++) {
- if(tmp == ccReturn.getChars().get(j)) {
- ccReturn.getChars().remove(j);
- break;
- }
- }
- }
- return ccReturn;
- }
- public boolean isSubset(CharCollection cc) {
- for(int i = 0; i < cc.size(); i++) {
- if(cc.count(cc.getChars().get(i)) > this.count(cc.getChars().get(i))) {
- return false;
- }
- }
- return true;
- }
- public List<Character> getChars() {
- return chars;
- }
- public void setChars(List<Character> chars) {
- this.chars = chars;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement