集合

作者: _____西班木有蛀牙 | 来源:发表于2018-07-04 13:44 被阅读15次
  • 集合
    指具有某种特定性质的具体的或抽象的对象汇总成的集体
  • 空集
    不包含任何元素
  • 子集
  • 并集
  • 交集
  • 补集
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>集合</title>
</head>
<body>
    <script>
        function Set() {
            this.dataStore = [];
            this.add = add;
            this.remove = remove;
            this.show = show;
            this.union = union;
            this.intersect = intersect;
            this.difference = difference;
            this.contains = contains;
            this.size = size;
            this.subset = subset;
        }

        function add(data) {
            if (this.dataStore.indexOf(data) < 0) {
                this.dataStore.push(data);
            } else {
                return false
            }
        }

        function remove(data) {
            var pos = this.dataStore.indexOf(data);
            if (pos > -1) {
                this.dataStore.splice(pos, 1)
            } else {
                return false
            }
        }

        function show() {
            return this.dataStore;
        }

        function contains(data) {
            if (this.dataStore.indexOf(data) > -1) {
                return true;
            } else {
                return false;
            }
        }

        function union(set) {
            // 并集
            var temSet = new Set();
            for (var i = 0; i < this.size(); i++) {
                temSet.add(this.dataStore[i]);
            }
            for (i = 0; i < set.size(); i++) {
                if (!temSet.contains(set.dataStore[i])) {
                    temSet.dataStore.push(set.dataStore[i]);
                }
            }
            return temSet;
        }

        function intersect(set) {
            // 补集
            var temSet = new Set();
            for (var i = 0; i < this.size(); i++) {
                if (set.contains(this.dataStore[i])) {
                    temSet.add(this.dataStore[i])
                }
            }
            return temSet;
        }

        function difference(set) {
            // 补集
            var temSet = new Set();
            for (var i = 0; i < this.size(); i++) {
                if (!set.contains(this.dataStore[i])) {
                    temSet.add(this.dataStore[i])
                }
            }
            return temSet;
        }

        function size() {
            return this.dataStore.length;
        }

        function subset(set) {
            // set是否是当前的子集
            if (set.size() > this.size()) {
                return false
            } else {
                for (var i = 0; i < set.size(); i++) {
                    if (!this.contains(set.dataStore[i])) {
                        return false;
                    }
                }
                return true;
            }
        }
        var names = new Set();
        names.add('晓红');
        names.add('小明');
        names.add('小李');
        names.add('小张');
        console.log(names.show());
        names.remove('小张');
        console.log(names.show());


        var cis = new Set();
        cis.add('晓红');
        // cis.add('ahs');
        // cis.add('abi');
        // cis.add('ahe');
        console.log('cis:', cis.show());

        names.subset(cis); // true

        console.log('并集', cis.union(names));
        console.log('交集', cis.intersect(names));
        console.log('补集', cis.difference(names));
    </script>
</body>
</html>

相关文章

  • 我的Swift的学习总结 -->第二周

    集合 集合:Set,定义一个集合可以写成:var 集合名 : Set<集合类型> = [集合元素],具体的集合应用...

  • markdown 测试

    集合 集合 集合 引用

  • kotlin学习第五天:集合,高阶函数,Lambda表达式

    集合 list集合 list集合分为可变集合与不可变集合。由list of创建的集合为不可变集合,不能扩容,不能修...

  • kotlin练习 ---- 集合练习

    kotlin练习 - 集合练习 Set集合 Set集合创建 Set集合的使用 List集合 List集合创建 Li...

  • 集合总结

    集合 集合分为单列集合和双列集合两种: 一.单列集合: Collection是单列集合的顶级接口: 其中有三类集合...

  • 映射、元组、集合

    映射 元组 集合 集合之seq 集合之set 集合之map

  • 16.Collection集合

    主要内容: Collection 集合 迭代器 增强for List 集合 Set 集合 1,集合 集合是java...

  • 集合与有序集合

    集合分为有序集合 (zset) 和无序集合 (set), 一般无序集合也直接说成集合 无序集合 (set) 无序集...

  • python入坑第八天|集合

    好的,各位蛇友,我们今天来学习集合。 内容: 集合的创建 集合操作符号 集合的内置函数 集合的创建 集合用set(...

  • 集合框架

    集合框架的概念 集合:存放数据的容器 集合框架:java中,用于表示集合,以及操作集合的类和接口的统称 数组与集合...

网友评论

      本文标题:集合

      本文链接:https://www.haomeiwen.com/subject/qrdhuftx.html