从方法返回值并在另一个类中使用它 - Java

时间:2016-01-13 01:17:23

标签: java

目前在我的Maze课程中我有两种方法

public Set<Integer> getNode(int node) {
    return Collections.unmodifiableSet(adjList.get(node));
}

public int getNumberOfNodes() {
        return adjList.size();
    }

我正试图将当前节点拉入我的DFS课程,但我似乎错过了一些东西。目前我的DFS课程有这个。

private int[] route;
private boolean[] visited;


public DFS(Maze maze, int input) {
        int startNode = 0;
        int goalNode = 1;
        route = new int[maze.getadjList(node)]; << errors here
        visited = new boolean[maze.adjList.getNode()]; << errors here
        //Takes user's input and runs desired function
        if(input == 1){
        findOne(maze, startNode, goalNode);
        }
        else if (input == 2){
        findAll(maze, startNode, goalNode);
        }
        else {
            System.out.println("input invalid. No Solution Returned");
        }
    }

如果我能提供帮助,请随时向我询问。感谢

如果需要,

完整Maze课程:

import java.io.*;
import java.util.*;

public class Maze {

    final Map<Integer, Set<Integer>> adjList = new HashMap<>();
    /**
     * The main constructor that takes a String for reading maze file.
     *
     * @param file
     */
    public Maze(File file) throws FileNotFoundException {
        try (Scanner scan = new Scanner(file)) {
            while (scan.hasNextInt()) {
                int node1 = scan.nextInt();
                int node2 = scan.nextInt();
                this.connect(node1, node2);
                this.connect(node2, node1);
            }
        }
    }



    /**
     * Makes a unidirectional connection from node1 to node2.
     */
    private void connect(int node1, int node2) {
        if (!this.adjList.containsKey(node1)) {
            this.adjList.put(node1, new HashSet<Integer>());
        }
        this.adjList.get(node1).add(node2);
    }

    /**
     * Returns a human-readable description of the adjacency lists.
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Map.Entry<Integer, Set<Integer>> adj : this.adjList.entrySet()) {
            int from = adj.getKey();
            Set<Integer> to = adj.getValue();
            s.append(from).append(" connected to ").append(to).append('\n');
        }
        return s.toString();
    }

    /**
     * Returns the set of nodes connected to a particular node.
     *
     * @param node - the node whose neighbors should be fetched
     */
    public Iterable<Integer> getadjList(int node) {
        return Collections.unmodifiableSet(adjList.get(node));
    }

    public Set<Integer> getNode(int node) {
        return Collections.unmodifiableSet(adjList.get(node));
    }

    public int getNumberOfNodes() {
        return adjList.size();
    }

    /**
     * Demonstration of file reading.
     */
    public static void main(String[] args) throws FileNotFoundException {
        System.err.print("Enter File: ");
        Scanner scanFile = new Scanner(System.in);
        String file = scanFile.nextLine();
        Maze m = new Maze(new File(file));
        System.out.println(m);

    }

}

2 个答案:

答案 0 :(得分:0)

这些语句看起来像是在尝试创建一个新的class FooBase { private: std::atomic<uint32_t> m_retainCount; public: FooBase() { m_retainCount = 1; }; virtual ~FooBase() { assert(m_retainCount == 0); // Prevent from direct delete without release() //clean-up if any }; void *release() { m_retainCount--; if (!m_retainCount) delete this; return this; }; void *retain() { m_retainCount++; return this; }; }; 数组和一个新的int数组:

boolean

但是要创建数组,方括号之间的参数必须是route = new int[maze.getadjList(node)]; << errors here visited = new boolean[maze.adjList.getNode()]; << errors here 。 它的值给出了数组的大小。

您正在调用的函数不会返回int,因此您会收到错误。

答案 1 :(得分:0)

正如@Ian Mc在评论中所述,首先使用。

启动数组解决了问题
route = new int[maze.getNumberOfNodes()]
visited = new boolean[ maze.getNumberOfNodes()]