会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133651个问题

2020 12 10---------

package com.bjsxt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基于树形结构实现元素存储的容器
 */
public class MyTree<E> {
    private Map<E, E> map = new HashMap<>();//String--->String
    private Map<E, List<E>> map2 = new HashMap<>();//String ---->List

    /**
     * 向容器中添加元素
     */
    public void add(E parent, E item) {
        //完成在单结点之间的映射
        this.map.put(item, parent);
        //完成多结点之间的映射
        List<E> list = this.map2.get(parent);
        //判断当前结点下是否含有子结点,如果没有则创建一个新的List
        if (list == null) {
            list = new ArrayList<>();
            this.map2.put(parent, list);
        }
        list.add(item);
    }

    /**
     * 获取当前结点的父结点
     */
    public E getParent(E item) {
        return this.map.get(item);
    }

    /**
     * 获取当前结点的子结点
     */
    public List<E> getChild(E item) {
        return this.map2.get(item);
    }

    /**
     * 获取当前结点的兄弟结点
     */
    public List<E> getBrother(E item) {
        //获取当前结点的父结点
        E parent = this.getParent(item);
        //获取当前父结点的所有的子结点
        List<E> list = this.getChild(parent);
        List<E> brother = new ArrayList<>();
        if (list != null) {
            brother.addAll(list);
            brother.remove(item);
        }
        return brother;
    }

    /**
     * 获取当前结点的祖先结点
     */
    public List<E> getForefathers(E item) {
        //获取当前结点的父结点
        E parent = this.getParent(item);
        //结束递归的边界条件
        if (parent == null) {
            return new ArrayList<>();
        }
        //递归调用,再次获取当前结点父结点的父结点
        List<E> list = this.getForefathers(parent);
        //将递归到的所有结点元素添加到返回的List中
        list.add(parent);
        return list;
    }

    /**
     * 获取当前结点的子孙结点
     */
    public List<E> getGrandChildren(E item) {
        //存放所有子孙结点中的元素
        List<E> list = new ArrayList<>();
        //获取当前结点的子结点
        List<E> child = this.getChild(item);
        //结束递归的边界条件
        if (child == null) {
            return list;
        }
        //遍历子结点
        for (int i = 0; i < child.size(); i++) {
            //获取节点中的元素
            E ele = child.get(i);
            List<E> temp = this.getGrandChildren(ele);
            list.add(ele);
            list.addAll(temp);
        }
        return list;
    }

    public static void main(String[] args) {
        //实例化容器
        MyTree<String> myTree = new MyTree<>();
        //添加元素
        myTree.add("root", "生物");
        myTree.add("生物", "植物");
        myTree.add("生物", "动物");
        myTree.add("生物", "菌类");
        myTree.add("动物", "脊椎动物");
        myTree.add("动物", "脊索动物");
        myTree.add("动物", "腔肠动物");
        myTree.add("脊椎动物", "哺乳动物");
        myTree.add("脊椎动物", "鱼类");
        myTree.add("哺乳动物", "猫");
        myTree.add("哺乳动物", "牛");
        myTree.add("哺乳动物", "人");
        System.out.println("---------获取父结点---------");
        String parent = myTree.getParent("鱼类");
        System.out.println(parent);
        System.out.println("---------获取子结点---------");
        List<String> child = myTree.getChild("动物");
        for (int i = 0; i < child.size(); i++) {
            System.out.println(child.get(i));
        }
        System.out.println("---------获取兄弟结点---------");
        List<String> brother = myTree.getBrother("脊椎动物");
        for (int i = 0; i < brother.size(); i++) {
            System.out.println(brother.get(i));
        }
        System.out.println("---------获取祖先结点---------");
        List<String> foreFathers = myTree.getForefathers("人");
        for (int i = 0; i < foreFathers.size(); i++) {
            System.out.println(foreFathers.get(i));
        }
        System.out.println("---------获取子孙结点---------");
        List<String> grandChildren = myTree.getGrandChildren("root");
        for (int i = 0; i < grandChildren.size(); i++) {
            System.out.println(grandChildren.get(i));
        }

    }
}

获取当前节点的祖先节点中 为什么要return new ArrayList<>();    里面老师说相当于用一个容器把递归的元素装起来然后返回,不用容器可以吗?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 27181楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 27182楼
Python 全系列/第一阶段:Python入门/控制语句 27183楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 27184楼
Python 全系列/第一阶段:Python入门/面向对象 27185楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/HTML5基础元素 27186楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/变量、数据类型、运算符 27188楼

问题一:centos7 下载 docker-ce repo出错,按类似文章操作还是解决不了https://blog.csdn.net/qq_27327261/article/details/108835620


问题二:上述无果,换centos8,一直都没有网络,从刚开始学linux开始到现在都没解决,对着老师视频重装修改无数次还是没用(老师视频里面在安装时没有设置网卡信息),后来对着这个视频:https://www.bilibili.com/video/BV11p4y1i7hv?from=search&seid=2353520204649742328  ,安装centos7就没问题。

两个问题求老师帮帮忙啊!

image.png




image.png

image.png

image.png






JAVA 全系列/第二十四阶段:容器管理技术/Docker 27191楼
Python 全系列/第二十阶段:数据分析-数据管理/numpy 27192楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解决浏览器事件绑定兼容性问题</title>
    <style>
        .div1 {
            width: 200px;
            height: 200px;
            background-color: paleturquoise;
        }
        .div2 {
            margin: 25px auto;
            width: 100px;
            height: 100px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="div1">div1
        <div class="div2">div2</div>
    </div>
<script>
    var div1=document.querySelector('.div1');
    var div2=document.querySelector('.div2');

    function func () {
        console.log('这是div');
    }
    // function func2 () {
    //     console.log('IE里的div');
    // }

    var cleanBug={
        add:function (ele,type,func) {
                if (ele.addEventListener) {
                    ele.addEventListener(type,func);
                }else {
                    ele.attachEvent('on'+type,func);
                }
            },
        remove:function (ele,type,func) {
                    if (ele.removeEventListener) {
                        ele.removeEventListener(type,func);
                    }else {
                        ele.detachEvent('on'+type,func);
                    }
                }    
            }

    cleanBug.add(div1,'click',func);
    // (div2,'click',func);
</script>    
</body>
</html>

老师,我问两个问题:

1、如果两个div是嵌套关系,我给外面的div敲击事件,里面的div点击它也会产生敲击事件?

2、我想给div1和div2各一个敲击事件且让他们打印的值不一样,代码该怎么修改?

WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 27193楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637