Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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优先级队列错误 public lightsoutbbs(){ //阅读文本 int[]arr=新int[25]; int[]状态=新int[25]; int[]soln=新的int[25]; 布尔检查=真; PriorityQueue q=新的PriorityQueue(); //读取文本文件 试一试{ FileInputStream fstream=新的FileInputStream(“switches.txt”); DataInputStream in=新的DataInputStream(fstream); BufferedReader br=新的BufferedReader(新的InputStreamReader(in)); 弦斯特林; int i=0; //逐行读取文件 而((strLine=br.readLine())!=null){ //标记化strline StringTokenizer st=新的StringTokenizer(strLine,“\n”); 而(st.hasMoreTokens()){ arr[i]=Integer.parseInt(st.nextToken()); i++; } //关闭输入流 }in.close(); }catch(异常e){//catch异常(如果有) System.err.println(“错误:+e.getMessage()); } 对于(int i=0;i_Java_Priority Queue - Fatal编程技术网

Java优先级队列错误 public lightsoutbbs(){ //阅读文本 int[]arr=新int[25]; int[]状态=新int[25]; int[]soln=新的int[25]; 布尔检查=真; PriorityQueue q=新的PriorityQueue(); //读取文本文件 试一试{ FileInputStream fstream=新的FileInputStream(“switches.txt”); DataInputStream in=新的DataInputStream(fstream); BufferedReader br=新的BufferedReader(新的InputStreamReader(in)); 弦斯特林; int i=0; //逐行读取文件 而((strLine=br.readLine())!=null){ //标记化strline StringTokenizer st=新的StringTokenizer(strLine,“\n”); 而(st.hasMoreTokens()){ arr[i]=Integer.parseInt(st.nextToken()); i++; } //关闭输入流 }in.close(); }catch(异常e){//catch异常(如果有) System.err.println(“错误:+e.getMessage()); } 对于(int i=0;i

Java优先级队列错误 public lightsoutbbs(){ //阅读文本 int[]arr=新int[25]; int[]状态=新int[25]; int[]soln=新的int[25]; 布尔检查=真; PriorityQueue q=新的PriorityQueue(); //读取文本文件 试一试{ FileInputStream fstream=新的FileInputStream(“switches.txt”); DataInputStream in=新的DataInputStream(fstream); BufferedReader br=新的BufferedReader(新的InputStreamReader(in)); 弦斯特林; int i=0; //逐行读取文件 而((strLine=br.readLine())!=null){ //标记化strline StringTokenizer st=新的StringTokenizer(strLine,“\n”); 而(st.hasMoreTokens()){ arr[i]=Integer.parseInt(st.nextToken()); i++; } //关闭输入流 }in.close(); }catch(异常e){//catch异常(如果有) System.err.println(“错误:+e.getMessage()); } 对于(int i=0;i,java,priority-queue,Java,Priority Queue,从错误消息中很明显,您的程序失败了,因为您使用的Node类没有实现Comparable接口。没有Comparable接口PriorityQueue将不知道如何对元素(Node对象)排序 解决方案: 使您的节点类实现compariable接口并重写public int comparieto(Obj o);基于某个id/优先级比较节点(我不知道节点类的定义,但可能是x.depth?) public节点实现可比较的{ ... @凌驾 公共整数比较(节点o){ 返回this.priority>o.pri

从错误消息中很明显,您的程序失败了,因为您使用的
Node
类没有实现
Comparable
接口。没有
Comparable
接口
PriorityQueue
将不知道如何对元素(
Node
对象)排序

解决方案:

使您的节点类实现
compariable
接口并重写public int comparieto(Obj o);基于某个id/优先级比较节点(我不知道节点类的定义,但可能是x.depth?)

public节点实现可比较的{
...
@凌驾
公共整数比较(节点o){
返回this.priority>o.priority;
}
}

哪一行给出了错误?它没有明确说明哪一行有错误,只是打印了ClassCastException
public LightsOutBFS(){
    //readtext
    int[] arr = new int[25];
    int[] state = new int[25];
    int[] soln = new int[25];

    boolean check=true;
    PriorityQueue<Node> q = new PriorityQueue<Node>();

    //Reading the text file
    try{
        FileInputStream fstream = new FileInputStream("switches.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int i = 0;

        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
                //tokenize strline
                StringTokenizer st = new StringTokenizer(strLine, " \n"); 
                while(st.hasMoreTokens()) { 
                arr[i]=Integer.parseInt(st.nextToken());
                i++;
            }
        //Close the input stream
        }in.close();


    }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
    }
    for(int i=0; i<25; i++){
        state[i]=0;
        soln[i]=0;
    }
    //loop that flips the adjacent side of switches turned on
    for(int i=0;i<25;i++){
        if(arr[i]==1)
            method.flip_adjacent(i,state);
    }


    //implement bfs here
    parent = new Node(state,0,soln,null);


    q.offer(parent);
    while(check){
        while(!q.isEmpty()){
            Node x = q.poll();
            int depth = x.depth;
            int posMoves = 25-depth;
            for(int i=0; i<posMoves;i++){
                current = generateNode(x.state,depth,x.soln,x);
                if(EtoNaYun(current.state))
                    check=false;//check state;
                q.offer(current);
            }
        }
    }

}
public Node implements Comparable<Node> {
   ...
   @Override
   public int compareTo(Node o) {
    return this.priority > o.priority;
   }
}