Java 使用.contains()搜索哈希集

Java 使用.contains()搜索哈希集,java,Java,我有一组字符串,用于表示RushHour状态。哈希集包含以前访问过的所有状态 我调用函数返回给定状态下可用的下一步移动,并检查是否已使用代码访问它们: if(!(visitedHash.contains(childBoards.get(i.Convert()))) 然后,如果该状态尚未被访问,我将其添加到队列中,以使用广度优先搜索进行求解 问题是当我编写这行代码时: if(visitedHash.contains(currentBoard.Convert())){ System.out.prin

我有一组字符串,用于表示RushHour状态。哈希集包含以前访问过的所有状态

我调用函数返回给定状态下可用的下一步移动,并检查是否已使用代码访问它们:

if(!(visitedHash.contains(childBoards.get(i.Convert())))

然后,如果该状态尚未被访问,我将其添加到队列中,以使用广度优先搜索进行求解

问题是当我编写这行代码时:

if(visitedHash.contains(currentBoard.Convert())){
System.out.println(“发生了什么事!!??”);
}

当我投票选出一个新的董事会时,许多州都在打印“发生了什么事!!??”

这是不可能的!应该吗?我刚刚检查了它们是否包含在队列中,并将它们添加到队列中,因此必须通过if语句

以下是我的搜索方法的完整代码:

public void search(Board b){    
    //--------Perform Breadth First Search on Board b--------//Method to solve the puzzle using Breadth First Search
    System.out.println("Attempting to solve the Board using Breadth First Search...");
    long startTime = System.nanoTime();                                                     //capture the start time for the method
    queue.add(b);                                                                           //push the input board onto the front of the queue
    while(!solved){                                                                         //while the board is not solved
        currentBoard = queue.poll();                                                        //assign the top of the queue to currentBoard
        currentBoard.print();                                                               //print the board to the screen (PRINT EVERY BOARD VISITED)
        System.out.println(currentBoard.Convert());
        System.out.println("This board is on level " + getLevel(currentBoard));
        //System.out.println("Visited Boards size: " + visitedHash.size());
        if(visitedHash.contains(currentBoard.Convert())){
            System.out.println("Whats Going on!!??");
        }
        boardsExplored++;                                                                   //increment the number of boardsExplored
        if(currentBoard.isGoal()){                                                          //if the board is the goal state
            long endTime = System.nanoTime();                                               //capture the end time for the method
            long duration = endTime - startTime;                                            //calculate the duration for the method
            time = (double)duration / 1000000000.0;                                         //convert the time to seconds
            System.out.println("SOLVED! Goal car is Free!");
            System.out.println("Time taken to solve = " + time + " seconds");               //print the time taken in seconds
            System.out.println("Moves made = " + (boardsExplored - 1));                     //print the number of boards explored to reach goal
            visitedHash.add(currentBoard.Convert());                                        //add the board (when converted to a string) to the list of visited boards              
            printSolution(currentBoard);                                                    //Call method to print shortest path found
            write(b);                                                                       //Call method to write data to the file
            solved = true;                                                                  //set solved to true
            return;                                                                         //exit the loop
        }
        visitedHash.add(currentBoard.Convert());                                            //add the board (when converted to a string) to the list of visited boards              
        childBoards = currentBoard.getChildMoves();                                         //call getChildMoves on the currentBoard to retrieve all available boards
        for (int i = 0 ; i < childBoards.size() ; i ++){                                    //for every one of the child boards
            if(!(visitedHash.contains(childBoards.get(i).Convert()))){                      //if the child board has NOT previously been visited
                queue.add(childBoards.get(i));                                              //add the child board to the queue and loop back    
                parent.put(childBoards.get(i), currentBoard);                               //Map the child board to its parent
                level.put(childBoards.get(i), getLevel(currentBoard));
            }
        }
    }
}
publicsvoid搜索(Board b){
//--------在板b上执行广度优先搜索-----------//使用广度优先搜索解决此难题的方法
System.out.println(“尝试使用广度优先搜索解决电路板问题…”);
long startTime=System.nanoTime();//捕获方法的开始时间
queue.add(b);//将输入板推到队列的前面
while(!solved){//当线路板未求解时
currentBoard=queue.poll();//将队列的顶部分配给currentBoard
currentBoard.print();//将电路板打印到屏幕上(打印访问的每个电路板)
System.out.println(currentBoard.Convert());
System.out.println(“此板位于标高”+getLevel(currentBoard));
//System.out.println(“已访问的板大小:+visitedHash.size());
if(visitedHash.contains(currentBoard.Convert())){
System.out.println(“发生了什么事!!??”);
}
boardsExplored++;//增加boardsExplored的数量
if(currentBoard.isGoal()){//如果电路板是目标状态
long endTime=System.nanoTime();//捕获方法的结束时间
long duration=endTime-startTime;//计算方法的持续时间
时间=(双精度)持续时间/100000000.0;//将时间转换为秒
System.out.println(“解决了!目标车免费!”);
System.out.println(“求解所用的时间=”+时间+“秒”);//打印所用的时间(秒)
System.out.println(“Moves made=“+(boardsExplored-1));//打印为达到目标而探索的板数
visitedHash.add(currentBoard.Convert());//将线路板(转换为字符串时)添加到已访问线路板列表中
printSolution(currentBoard);//调用方法打印找到的最短路径
write(b);//调用方法将数据写入文件
solved=true;//将solved设置为true
return;//退出循环
}
visitedHash.add(currentBoard.Convert());//将线路板(转换为字符串时)添加到已访问线路板列表中
childBoards=currentBoard.getChildMoves();//在currentBoard上调用getChildMoves以检索所有可用的板
对于(int i=0;i
正如其他人在评论中提到的,您可能需要为Convert()的返回类型创建适当的hashCode和equals方法。

Convert做什么?确保Convert()的返回类型实现了
hashCode()
和相应的
equals(…)
方法。然后,checl if contains在您将元素放入
哈希集
后直接查找该元素,以测试该集是否与您的元素正常工作。您是否为
Convert()的返回类型实现了
equals
?如果没有,请同时发布您的
Board
类。您是否想过在
If(visitedHash.contains(currentBoard.Convert())
条件中添加
else
块?当(!solved)
loopConvert将RushHour线路板简单地转换为字符串时,您的控制台可能没有在
的第一次迭代中打印“What’s Going!!??”。“-”表示空格,然后是字母表示车辆为什么我需要我需要处理HashCode和equals方法?我只是比较一个字符串和一个l