猿问

如何修改下面的代码,在线等?

我需要把下面这个有缺失的代码实现,

  1. 在这里,这个代码的构造函数是不完整的,如何合理的完成构造函数的填充

  2. 在完成构造函数部分之后,我需要在主函数中创建几个节点, 然后使用findChild方法来找到每个节点的名字。怎么把findChild函数填充完整,这样程序可以顺利进行下面是代码

 package directory_explorer;


import java.util.Map;

import java.util.Collection;

import java.util.HashMap;


/**

 * The root of a tree representing a directory structure.

 */

public class FileNode {


    /** The name of the file or directory this node represents. */

    private String name;

    /** Whether this node represents a file or a directory. */

    private FileType type;

    /** This node's parent. */

    private FileNode parent;

    /**

     * This node's children, mapped from the file names to the nodes. If type is

     * FileType.FILE, this is null.

     */

    private Map<String, FileNode> children;


    /**

     * A node in this tree.

     *

     * @param name

     *            the file

     * @param parent

     *            the parent node.

     * @param type

     *            file or directory

     * @see buildFileTree

     */

    public FileNode(String name, FileNode parent, FileType type) {

        this.name = name;

        // TODO: complete this method.

    }


    /**

     * Find and return a child node named name in this directory tree, or null

     * if there is no such child node.

     *

     * @param name

     *            the file name to search for

     * @return the node named name

     */

    public FileNode findChild(String name) {

        FileNode result = null;

        // TODO: complete this method.

        return result;

    }


    /**

     * Return the name of the file or directory represented by this node.

     *

     * @return name of this Node

     */

    public String getName() {

        return this.name;

    }


    /**

     * Set the name of the current node

     *

     * @param name

     *            of the file/directory

     */

    public void setName(String name) {

        this.name = name;

    }

HUWWW
浏览 508回答 3
3回答

小唯快跑啊

public FileNode findChild(String name){&nbsp; &nbsp; if(this.children!=null){&nbsp; &nbsp; &nbsp; &nbsp; if(name!=null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileNode fileNode = null ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //此处每个节点&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String key : children.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(key.equals(name)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileNode = children.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return fileNode;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答