繁体   English   中英

最后添加的元素将覆盖列表中的所有对象。 爪哇

[英]The last element added overwrites all the objects in the List. Java

在循环中,我检查了res(给定操作a的下一个状态)的内容,它们是正确的。 它确实添加到边界列表,但是值都是相同的,即res的最后一次迭代。

public State BFSearch(){     
         State initial = new State(array);
         LinkedList<State> frontier = new LinkedList<State>();
         frontier.add(initial);


         while(!frontier.isEmpty()){
            State currentState = new State();
            currentState = frontier.removeFirst();

            if(goalTest(currentState)){
                System.out.println("goal");
                return currentState;
            }
            else{
                for (Point a : Actions(currentState)) {
                    State res = new State();
                    res = Result(currentState,a);
                    frontier.add(res);
                }

            }
         }
         return null; 
    }

我想您已尝试实现“ 广度优先”搜索

在您的实现中,您似乎错过了这一步: if n is not in S:这样就可以工作,即使您的图形中没有循环。 在其他情况下,您将陷入无限循环。

另外,此代码中有很多错误:

public State BFSearch(){     
     State initial = new State(array);
     LinkedList<State> frontier = new LinkedList<State>();
     frontier.add(initial); <-- initially you have 1 element in list


     while(!frontier.isEmpty()){
        State currentState = new State(); <-- You don't need to create new instance because you'll overwrite it on next step
        currentState = frontier.removeFirst(); <-- you remove initial element

        if(goalTest(currentState)){
            System.out.println("goal");
            return currentState;
        }
        else{
            for (Point a : Actions(currentState)) { <-- I guess Actions is method which returns N points
                State res = new State(); <-- You don't need to create instance because you overwrite it on next step
                res = Result(currentState,a); <-- is it method with just return state or you just missed `new` 
                 <-- here should be check for existent `res` in `frontier` -->

                frontier.add(res); <-- there you put N new steps
            }

        }
     }
     return null; 
}

您的实现可能如下所示:

public State BFSearch() {
    State initial = new State(arrays);
    List<State> frontier = new LinkedList<>();
    frontier.add(initial);


    while (!frontier.isEmpty()) {
        State currentState = frontier.removeFirst();

        if (goalTest(currentState)) {
            System.out.println("goal");
            return currentState;
        } else {
            for (Point a : actions(currentState)) {
                State res = result(currentState, a);
                if (!frontier.contains(res)) {
                    frontier.add(res);
                }
            }

        }
    }
    return null;
}

重要说明确保在State实现中正确实现了equalshashCode方法

这在语句currentState = frontier.removeFirst();很明显currentState = frontier.removeFirst(); 在while循环中,最终所有元素都将被删除。 如果您不希望删除,则可以尝试使用peekFirst()方法。

很难确切地了解您在做什么或您的问题是什么,但是从我收集到的信息中,您正在删除第一个元素(从最初可能是单个元素列表的列表中删除),然后您检查它是否符合条件,则从函数返回它,如果不存在,则从另一个集合中填充元素。 然后重复以上操作,直到第一个元素是您想要的? 您是否尝试过在循环中打印出“ Result(currentState,a)”,看看它能为您带来什么? 还是在输入下一个迭代之前打印出列表?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM