利用所学去学习。离散数学里面又学到了以前的集合,“包并补”什么的。那么作为一个要做“程序猿”的大学生,不得用自己的专业去解决其中的问题?所以这个包装类——“Set”诞生了。

遇到的问题

1.怎么去表达一个“集合”?

2.怎么去解决“集合”的运算?

解决思路

1.一开始是真的无从下手,到底要怎么去弄这个“集合”?用一个字符串来表示一个集合吗?{1,2,3,4,5}这就是一个集合吗?想了一下,这里提到的集合是一个名词,是描述”集合“这个东西的概念。那么就可以用我们面向对象的思想来解释这个”集合“的概念了。

想到这里就确定了怎么去写这个”集合“了,通过对”集合“这个类的封装来表达”集合“的这个东西。一个对象就是一个集合。

2.集合的运算我的理解是 集合间元素的对比。所以基于这一点,整体的运算代码其就是对集合里元素的处理。目前我用的是for循环的嵌套,通过遍历来实现元素的对比,以后再尝试着的去优化。

整体代码

import java.util.*;

public class Set {
    // 字符串数组用以存储字符串元素
    private String[] a;

    // 默认为空集
    public Set() {
        a = new String[0];
    }

    // 构造方法
    public Set(String str) {
        StringTokenizer strToken = new StringTokenizer(str, "{,}");
        int num = strToken.countTokens();
        a = new String[num];
        for (int i = 0; i < num; i++) {
            a[i] = strToken.nextToken();
        }
    }

    // 读取集合
    public String getSet() {
        String str = "";
        for (int i = 0; i < a.length; i++) {
            str = str + "," + a[i];
        }
        return "{" + str.substring(1) + "}";
    }

    // 获取集合元素数量
    public int length() {
        return a.length;
    }

    // 交集
    public Set intersection(Set set) {
        String str = "";
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < set.a.length; j++) {
                if (a[i].equals(set.a[j]))
                    str = str + a[i] + ",";
            }
        }
        return new Set(str);
    }

    // 并集
    public Set union(Set set) {
        String str = "";
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < set.a.length; j++) {
                if (a[i].equals(set.a[j])) {
                    set.a[j] = "";
                    break;
                }
            }
            str = str + a[i] + ",";
        }
        for (int j = 0; j < set.a.length; j++) {
            str = str + set.a[j] + ",";
        }
        return new Set(str);
    }

    // 相等
    public boolean equal(Set set) {
        if (a.length == set.a.length) {
            int num = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < set.a.length; j++) {
                    if (a[i].equals(set.a[j])) {
                        num++;
                        break;
                    }
                }
            }
            if (num == a.length)
                return true;
        }
        return false;
    }

    // 包含
    public boolean subset(Set set) {
        if (a.length <= set.a.length) {
            int num = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < set.a.length; j++) {
                    if (a[i].equals(set.a[j])) {
                        num++;
                        break;
                    }
                }
            }
            if (num == a.length)
                return true;
        }
        return false;
    }

    // 补集
    public Set complement(Set set) {
        String str = "";
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < set.a.length; j++) {
                if (a[i].equals(set.a[j])) {
                    set.a[j] = "";
                    break;
                }
            }
        }
        for (int j = 0; j < set.a.length; j++) {
            str = str + set.a[j] + ",";
        }
        return new Set(str);
    }

    // 集合的差
    public Set difference(Set set, Set set1) {
        Set newSet = set.complement(set1);
        return this.intersection(newSet);
    }

    // 集合的对称差
    public Set symmetric(Set set, Set set1) {
        return this.union(set).difference(this.intersection(set), set1);
    }

}
最后修改:2022 年 04 月 16 日
如果觉得我的文章对你有用,请随意赞赏