Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示最短路径的用户输入_Java_User Input_Indexoutofboundsexception_Breadth First Search - Fatal编程技术网

Java 显示最短路径的用户输入

Java 显示最短路径的用户输入,java,user-input,indexoutofboundsexception,breadth-first-search,Java,User Input,Indexoutofboundsexception,Breadth First Search,我有一个边界排列异常,我不知道为什么。我试图让用户输入他们希望程序从哪个城市开始,然后让我的程序从该点开始执行广度优先搜索 我的程序一小时后到期,所以任何快速的帮助都会很棒 非常快,我的程序在邻接矩阵上执行广度优先搜索 谢谢各位,以下是我的主要观点: import java.util.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java

我有一个边界排列异常,我不知道为什么。我试图让用户输入他们希望程序从哪个城市开始,然后让我的程序从该点开始执行广度优先搜索

我的程序一小时后到期,所以任何快速的帮助都会很棒

非常快,我的程序在邻接矩阵上执行广度优先搜索

谢谢各位,以下是我的主要观点:

       import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.io.File;
import java.io.FileNotFoundException;

public class Main {


    public static void main(String[] args) 
    {



        //Lets create nodes 
        Node Seattle = new Node("Seattle");
        Node Vancouver=new Node("Vancouver");
        Node Portland = new Node("Portland");
        Node Houston = new Node("Houston");
        Node New_Orleans = new Node("New_Orleans");
        Node Miami = new Node("Miami");
        Node Omaha = new Node("Omaha");
        Node Louisville=new Node("Louisville");
        Node Boston = new Node("Boston");
        Node Boise = new Node("Boise ");
        Node Chicago = new Node("Chicago");
        Node Jacksonville = new Node("Jacksonville");
        Node Baltimore = new Node("Baltimore");
        Node Detroit = new Node("Detroit");
        Node Nashville =new Node("Nashville ");
        Node Oakland  = new Node("Oakland ");
        Node Denver= new Node("Denver ");
        Node Olympia = new Node("Olympia");
        Node Memphis = new Node("Memphis");

//
        //Create the graph, add nodes, create edges between nodes
        g.addNode(Seattle);
        g.addNode(Vancouver);
        g.addNode(Portland);
        g.addNode(Houston);
        g.addNode(New_Orleans);
        g.addNode(Miami);
        g.addNode(Omaha);
        g.addNode(Louisville);
        g.addNode(Boston);
        g.addNode(Boise);
        g.addNode(Chicago);
        g.addNode(Jacksonville);
        g.addNode(Baltimore);
        g.addNode(Detroit);
        g.addNode(Nashville);
        g.addNode(Oakland);
        g.addNode(Denver);
        g.addNode(Olympia);
        g.addNode(Memphis);

//      Scanner sc = new Scanner(System.in);
//      String shortPath = sc.next();
//      Node spath = new Node(shortPath);


        g.connectNode(Louisville,Memphis);

        g.connectNode(Houston,Seattle);

        g.connectNode(Boston,Vancouver);

        g.connectNode(Boise,Seattle);
        g.connectNode(Boise,Detroit);
        g.connectNode(Boise,Oakland);

        g.connectNode(Chicago,Olympia);

        g.connectNode(Portland,Seattle);

        g.connectNode(Jacksonville, Vancouver);

        g.connectNode(Baltimore, Vancouver);

        g.connectNode(Detroit, Boise);

        g.connectNode(Seattle, Portland );
        g.connectNode(Seattle, Houston );
        g.connectNode(Seattle, Vancouver);
        g.connectNode(Seattle, Denver);
        g.connectNode(Seattle, Boise);

        g.connectNode(Nashville, Memphis);

        g.connectNode(Oakland, Boise);
        g.connectNode(Oakland, Vancouver);

        g.connectNode(Vancouver, Seattle);
        g.connectNode(Vancouver,Olympia);
        g.connectNode(Vancouver,Jacksonville);
        g.connectNode(Vancouver,Baltimore);
        g.connectNode(Vancouver,Oakland);
        g.connectNode(Vancouver,Boston);

        g.connectNode(Denver, Seattle);

        g.connectNode(Olympia,Vancouver);
        g.connectNode(Olympia,Omaha);
        g.connectNode(Olympia,Chicago);

        g.connectNode(Memphis,Nashville);
        g.connectNode(Memphis,Louisville);
        g.connectNode(Memphis,Omaha);

        g.setRootNode(Houston);
        //Perform the traversal of the graph
        System.out.println("Shortest path is ------------->");
        g.bfs();
        ;
//      }
//      else {
//          throw new InvalidInputException();
//      }

        long lEndTime = System.currentTimeMillis(); 
        long difference = lEndTime - lStartTime;
        System.out.println("\nElapsed Time: " + difference + " ms");



    }

}
节点类:

public class Node 
{   


    public String label;
    public String label2;
    public boolean visited=false;
    //public Object equals;
    public Node(String name)
    {
        this.label= name;
//      this.label2 = name2;
    }
}
还有我的图形类:

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


public class Graph 
{
    //added
//  Vertex[] adjLists;

    public Node rootNode;
    public ArrayList nodes=new ArrayList();
    public int[][] adjMatrix;//Edges will be represented as adjacency Matrix
    int size;

    public void setRootNode(Node city)
    {
        this.rootNode=city;
    }

    public Node getRootNode()
    {
        return this.rootNode;
    }

    public void addNode(Node city)
    {
        nodes.add(city);
    }

    //This method will be called to make connect two nodes
    public void connectNode(Node start,Node end)
    {
        if(adjMatrix==null)
        {
            size=nodes.size();
            adjMatrix=new int[size][size];
        }

        int startIndex=nodes.indexOf(start);
        int endIndex=nodes.indexOf(end);
        adjMatrix[startIndex][endIndex]=1;      //initializing matrix with size 1 by 1
        adjMatrix[endIndex][startIndex]=1;
    }

    private Node getUnvisitedChildNode(Node n)
    {

        int index=nodes.indexOf(n);             //index is = to index of nodes
        int j=0;
        while(j<size)
        {
            if(adjMatrix[index][j]==1 && ((Node)nodes.get(j)).visited==false)
            {
                return (Node)nodes.get(j);
            }
            j++;
        }
        return null;
    }

    //BFS traversal of a tree is performed by the bfs() function
    public void bfs()
    {

        //BFS uses Queue data structure
        Queue q=new LinkedList();
        q.add(this.rootNode);
        printNode(this.rootNode);
        rootNode.visited=true;
        while(!q.isEmpty())
        {
            Node n=(Node)q.remove();
            Node child=null;
            while((child=getUnvisitedChildNode(n))!=null)
            {
                child.visited=true;
                printNode(child);
                q.add(child);
            }
        }
        //Clear visited property of nodes
        clearNodes();
    }


    //Utility methods for clearing visited property of node
    private void clearNodes()
    {
        int i=0;
        while(i<size)
        {
            Node n=(Node)nodes.get(i);
            n.visited=false;
            i++;
        }
    }

    //Utility methods for printing the node's label
    private void printNode(Node rootNode2)
    {
        System.out.print(rootNode2.label+" ---> ");
    }





}
import java.io.FileNotFoundException;
导入java.util.*;
公共类图
{
//增加
//顶点列表;
公共节点rootNode;
公共ArrayList节点=新建ArrayList();
公共int[][]邻接矩阵;//边将表示为邻接矩阵
整数大小;
公共void setRootNode(节点城市)
{
this.rootNode=city;
}
公共节点getRootNode()
{
返回this.rootNode;
}
公共void addNode(节点城市)
{
添加(城市);
}
//将调用此方法以连接两个节点
公共无效连接节点(节点开始、节点结束)
{
如果(adjMatrix==null)
{
size=nodes.size();
adjMatrix=新整数[大小][大小];
}
int startIndex=nodes.indexOf(start);
int endIndex=nodes.indexOf(end);
adjMatrix[startIndex][endIndex]=1;//初始化大小为1乘1的矩阵
adjMatrix[endIndex][startIndex]=1;
}
私有节点getUnvisitedChildNode(节点n)
{
int index=nodes.indexOf(n);//index=to节点索引
int j=0;

而(j似乎在
Graph.getUnvisitedChildNode
中,您可以使用
节点.indexOf(n)
搜索
节点
数组。这首先在队列的第一个元素上完成,即根节点,这一切都很好

但是,根节点在
Main
中设置为
new node(shortPath)
(第130行,请参见编辑)。此新节点从未添加到数组中。Java不会查看此节点内的标签,但当前仅当它是
节点的同一实例时才会查看

您可以尝试通过将以下方法添加到
节点
类来解决此问题:

@Override
public boolean equals(Object o) {
    if(o == null) return false;
    if(!(o instanceof Node)) return false;
    return label.equals(((Node)o).label);
}
如果两个节点的标签相等,则此方法将表示两个节点相等。我希望这将修复您的树。我现在将尝试此方法,同时您将尝试解释我的代码的实际功能

编辑:您在这里修改了代码,它现在被注释掉并位于另一行


EDIT2:添加对
equals
方法的覆盖对我来说似乎很有效。

我们应该忽略注释行吗?我现在就要调试它。首先,我在Main的“lets create nodes”位中发现了两个未关闭的文本。如果将粘贴的代码复制到StackOverflow,可能需要首先修复这些文本。它还发现了对
g.connectNode
用三个参数而不是两个参数。老天爷/老天爷,你本可以做一点工作,至少可以显示出错误发生的地方。我更新了代码人员!你还可以包括错误堆栈跟踪吗?@Ghostkeeper你救了我!太感谢你了!我很快就把它交上来了!