簡體   English   中英

從方法返回值並在另一個類中使用它-Java

[英]Returning a value from a method and using it in another class - 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);

    }

}

這些語句看起來像它們試圖創建一個新的int數組和一個新的boolean數組:

route = new int[maze.getadjList(node)]; << errors here
visited = new boolean[maze.adjList.getNode()]; << errors here

但要創建數組,方括號之間的參數必須為int 它的值給出了數組的大小。

您正在調用的函數不會返回int ,因此會出現錯誤。

正如@Ian Mc在評論中指出的那樣,該問題已通過首先使用初始化數組來解決。

route = new int[maze.getNumberOfNodes()]
visited = new boolean[ maze.getNumberOfNodes()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM