会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132849个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 2386楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2387楼

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 基础深化和提高/数据结构 2388楼

问题:无法读取schema文档。

说明:老师,我的xml文件和xsd文件都是按照视频里讲述的编写的,两个文件也都放在了项目根目录下,但是在引入xsd文件的时候仍然报错。我的文件按照视频写过好几个版本了,文件位置也放到src目录下试过,都没有解决。烦请帮忙给解答一下,谢谢。

系统错误信息:

图片.png


图片.png


文件内容:

1、book.xml

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="{book.xsd}">
    <book id="1001">
        <name>一路向北</name>
        <author>一路</author>
        <price>98.6</price>
    </book>
    <book>
        <name>未来可期</name>
        <author>不告诉你</author>
        <price>99.9</price>
    </book>
</books>

2、book.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"></xs:element>
                            <xs:element name="author" type="xs:string"></xs:element>
                            <xs:element name="price" type="xs:double"></xs:element>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:positiveInteger"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2390楼

代码1:

package com.ljp.sleep_yield_join_stop;

public class MyThread implements Runnable {

    @Override
    public void run() {
//        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "-------------" + i);

        }
    }
}

代码2:

package com.ljp.sleep_yield_join_stop;

public class Test {
    public static void main(String[] args) {
        MyThread mtd = new MyThread();
        Thread t01 = new Thread(mtd, "整天都吃橘子!!!");
        Thread t02 = new Thread(mtd,"想吃肉夹馍!!!");
        t01.start();
        t02.start();
        for(int i=0;i<10;i++){
            if(i==3){
                try {
                    t01.join();
//                    t02.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "*********" + i);
        }
    }
}

老师,是不是可以这么理解:只要是在哪个线程(比如主线程)里调用join(),那这个线程(主线程)阻塞,等其他线程运行完以后,这个线程再运行。那再这个线程(主线程)里,谁调用join()都可以,比如代码里t01也可以,t02也可以,感觉他们效果都是一样的;两个同时调用join(),感觉效果也是一样的,t01和t02并没有先后顺序,作用只是将主线程阻塞了而已。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2394楼

this.size++报错哪里出问题了
/*
* 基于双向链表实现元素存储的容器
* */
public class MyDoublyLinkedList<E> implements MyList<E> {

    /*
    * 定义双向链表的节点对象
    * */
    class Node<E>{
         E item;
         Node<E> prev;      //记录前一个节点对象
         Node<E> next;      // 记录下一个节点对象
         Node(Node<E> prev,E item,Node<E> next){
             this.prev = prev;
             this.item = item;
             this.next = next;
         }
    }
    private  Node head;   //记录头节点
    private  Node tail;   //记录尾节点
    private  Node size;   //记录原元素个数

    /*
    * 向双向链表中添加元素的方法
    * */
    @Override
    public void add(E element) {
        this.linkLast(element);
    }

    /*
    * 将节点对象添加到双向链表尾部
    * */
    private void linkLast(E element){
        //获取尾节点
        Node t = this.tail;
        //创建节点对象
        Node<E> node = new Node<>(t,element,null);
        //将新节点定义为尾节点
        this.tail = node;
        if(t == null){
            this.head = node;
        }else{
            t.next = node;
        }
        this.size++
    }

    /*
    * 根据指定位置获取元素
    * */
    @Override
    public E get(int indxe) {
        return null;
    }

    /*
    * 返回元素个数
    * */
    @Override
    public int size() {
        return 0;
    }

    /*
    * 根据指定位置删除元素*/
    @Override
    public E remove(int index) {
        return null;
    }

    public static void main(String[] args) {

    }
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2395楼

老师,这里Thread01类中for循环的println()里我没加" i ",而是直接打印了线程名字,如下代码:

package com.ljp.Runnable;

public class Thread01 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName() + "------------------");
            // System.out.println(Thread.currentThread().getName() + "------------------" + i);
        }
    }
}

在主线程中,println()里面有" i ",如下代码:

package com.ljp.Runnable;

public class Test {
    public static void main(String[] args) {
        Thread01 t01 = new Thread01();
        Thread td01 = new Thread(t01,"北坡的八百标兵");
        td01.start();

        for(int i=0; i<10;i++){
            System.out.println(Thread.currentThread().getName() + "-------------------------------" + i);
        }
    }
}

运行结果,主线程结果总是在线程1运行结果后面,如下运行结果:

北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
main-------------------------------0
main-------------------------------1
main-------------------------------2
main-------------------------------3
main-------------------------------4
main-------------------------------5
main-------------------------------6
main-------------------------------7
main-------------------------------8
main-------------------------------9

但是Thread01类的println()加上 " i ",就不会出现上面情况,运行结果如下:

main-------------------------------0
北坡的八百标兵------------------0
北坡的八百标兵------------------1
main-------------------------------1
北坡的八百标兵------------------2
main-------------------------------2
北坡的八百标兵------------------3
北坡的八百标兵------------------4
北坡的八百标兵------------------5
main-------------------------------3
北坡的八百标兵------------------6
main-------------------------------4
北坡的八百标兵------------------7
main-------------------------------5
北坡的八百标兵------------------8
main-------------------------------6
北坡的八百标兵------------------9
main-------------------------------7
main-------------------------------8
main-------------------------------9

为什么?????

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2397楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2399楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 2400楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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