简体   繁体   English

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

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

I've check the contents of res(the next state given action a) while in the loop and they are correct. 在循环中,我检查了res(给定操作a的下一个状态)的内容,它们是正确的。 It does add to the frontier list but the values are all the same, the last iteration of res. 它确实添加到边界列表,但是值都是相同的,即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; 
    }

I guess you're tried to implement Breadth-first search . 我想您已尝试实现“ 广度优先”搜索

From your implementation it looks like you missed this step: if n is not in S: So it could works just if you don't have a loop in your graph. 在您的实现中,您似乎错过了这一步: if n is not in S:这样就可以工作,即使您的图形中没有循环。 In other case, you'll stuck in infinite loop. 在其他情况下,您将陷入无限循环。

Also, a lot of errors in this code: 另外,此代码中有很多错误:

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; 
}

Your implementation could looks like: 您的实现可能如下所示:

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;
}

Important Be sure that you implemented equals and hashCode methods properly in your State implementation 重要说明确保在State实现中正确实现了equalshashCode方法

That's quite evident in the statement currentState = frontier.removeFirst(); 这在语句currentState = frontier.removeFirst();很明显currentState = frontier.removeFirst(); As it is in while loop, eventually all the elements get removed. 在while循环中,最终所有元素都将被删除。 If you do not wish to remove, you may try the method peekFirst() . 如果您不希望删除,则可以尝试使用peekFirst()方法。

Hard to follow exactly what it is your doing or what your question is but from what i gather you're removing the first element (from a list that is presumably a single element list at first), then you're checking to see if it matches a criteria, you're returning it from the function if it is, if not you're populating with elements based off another collection. 很难确切地了解您在做什么或您的问题是什么,但是从我收集到的信息中,您正在删除第一个元素(从最初可能是单个元素列表的列表中删除),然后您检查它是否符合条件,则从函数返回它,如果不存在,则从另一个集合中填充元素。 Then repeating the above until the first element is the one you want? 然后重复以上操作,直到第一个元素是您想要的? Have you tried printing out "Result(currentState,a)" in the loop and see what it gives you? 您是否尝试过在循环中打印出“ Result(currentState,a)”,看看它能为您带来什么? or printing out the list before you enter the next iteration? 还是在输入下一个迭代之前打印出列表?

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

相关问题 列表中的对象具有相同的值-最后添加的元素的值相同 - Objects in List have the same value - that of the last element added Output 不会打印链表中的所有列表。 使用 java - Output does not print all of the list in linked list. Using java Java的。 抽象元素列表。 如何确定元素类型 - Java. Abstract elements list. How to determine element type 删除循环链表中的所有匹配项。 (爪哇) - Deleting all occurrences in a circular linked list. (java) ListCellRendererComponent()方法更新JList的所有对象(元素),类似于上一次添加的元素 - ListCellRendererComponent() method updates all the objects(elements) of JList similar to last added element 为什么我的哈希图用添加的最后一个值覆盖? - Why my hashmap overwrites with last value added? 从java集合中的给定列表中删除除最后一个元素之外的所有元素 - Deletes all but the last element from a given list in java collections 如何在JAVA中报告列表中最后一个元素的所有并列位置 - how to report all tied positions for the last element in the list in JAVA 自定义列表重复最后添加的元素 - Custom List duplicates last added element Java ArrayList元素被最后添加的元素覆盖 - Java ArrayList Elements are overridden by Last Added Element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM