Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author R4bbiT
- */
- /**
- *
- * 5. Übungsblatt (6. −− 13. Mai 2011)
- */
- public class StringSet {
- /* VARIABLEN */
- protected String[] a;
- protected int n;
- /* KONSTRUKTOREN */
- StringSet(int n){
- this.a = new String[n];
- this.n = n;
- }
- StringSet(int n, String s){
- this(n);
- this.insert(s);
- }
- /* METHODEN */
- int capacity(){
- return this.n;
- }
- int card(){
- int i, x=0;
- for(i=0; i<this.a.length; i++){
- if(this.a[i] != null) x++;
- }
- return x;
- }
- public void print(){
- int i, x=0;
- System.out.print("{ ");
- for(i=0; i<this.a.length; i++){
- if(this.a[i] != null){
- System.out.print((x > 0 ? ", " : "" ) + a[i]);
- x++;
- }
- }
- System.out.print(" }");
- }
- boolean contains(String s){
- int i;
- for(i=0; i<this.a.length; i++){
- if(this.a[i] != null){
- if(this.a[i].equals(s)) return true;
- }
- }
- return false;
- }
- boolean insert(String s){
- int i;
- if(this.contains(s)) return true;
- for(i=0; i<this.a.length; i++){
- if(this.a[i] == null){
- this.a[i] = s;
- return true;
- }
- }
- return false;
- }
- boolean remove(String s){
- int i;
- for(i=0; i<this.a.length; i++){
- if(this.a[i] != null){
- if(this.a[i].equals(s)){
- this.a[i] = null;
- return true;
- }
- }
- }
- return false;
- }
- private StringSet gen_inter(StringSet s){
- int i, x=0;
- for(i=0; i<this.a.length; i++){
- if(s.contains(this.a[i])) x++;
- }
- StringSet ns = new StringSet(x);
- for(i=0; i<this.a.length; i++){
- if(s.contains(this.a[i])) ns.insert(this.a[i]);
- }
- return ns;
- }
- public static StringSet intersection(StringSet first, StringSet second){
- return first.gen_inter(second);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement