将元素添加到ArrayList,如果元素在ArrayList中具有相同的值,则无法添加它

时间:2017-03-29 14:58:13

标签: java arraylist

我正在开发一个搜索系统。我有这个问题,同一个Coordinate多次添加到搜索队列中。我添加了一些if语句来过滤掉这些问题,但它仍然会发生,我不知道为什么。我找不到使用LinkedHashSet的方法,因为我试图将新对象添加到列表中。

    package pathFinding;

    import java.util.ArrayList;
    import java.util.LinkedHashSet;

    public class SearchOld {
    public static Boolean pathFind(Coord start, Coord end /*, ArraList<Coord> blocked*/){

        boolean arrive = false;
        boolean way = false;

        ArrayList<Node> openList = new ArrayList<Node>();
        ArrayList<Node> closedList = new ArrayList<Node>();
        ArrayList<Node> adjNode = new ArrayList<Node>();
        ArrayList<Coord> emptyList = new ArrayList<Coord>();

        int num = 0;
        Node startNode = new Node(start, start);
        closedList.add(startNode);
        openList.add(startNode);
        System.out.println(openList.get(0).getCoord().getX() + " " + openList.get(0).getCoord().getY());
        a:{while(!arrive){
            all:{for(int i = 0;i<openList.size();i++){
                if(end.getX() == openList.get(i).getCoord().getX() && end.getY() == openList.get(i).getCoord().getY()){
                    arrive = true;
                    break all;
            }
            adjNode.addAll(Adjacent.findAdjacent(openList.get(0).getCoord()));
                for(int j=0;j<adjNode.size();j++){
                    check:{for(int k = 0;k<openList.size();k++){
                        if(adjNode.get(0).getCoord().getX() == openList.get(k).getCoord().getX() && adjNode.get(0).getCoord().getY() == openList.get(k).getCoord().getY()){
                            adjNode.remove(0);
                            System.err.println("X" + adjNode.get(0).getCoord().getX() + "Y" + adjNode.get(0).getCoord().getY());
                            arrive = true;
                            break check;
                        }else{
                            for(int l = 0;l<closedList.size();l++){
                                if(adjNode.get(0).getCoord().getX() == closedList.get(l).getCoord().getX() && adjNode.get(0).getCoord().getY() == closedList.get(l).getCoord().getY()){
                                    /*if(end.getX() == adjNode.get(0).getCoord().getX() && end.getY() == adjNode.get(0).getCoord().getY()){
                                        openList.add(adjNode.get(0));
                                        closedList.add(adjNode.get(0));
                                        break all;
                                }*/
                                    adjNode.remove(0);
                                    break check;
                                }else{
                                    openList.add(adjNode.get(0));
                                    adjNode.remove(0);
                                    break check;
                                }
                            }
                        }
                    }}
                }
            closedList.add(openList.get(0));
            openList.remove(0);
            LinkedHashSet<Node> tempSet = new LinkedHashSet<Node>();
            tempSet.addAll(closedList);
            closedList.clear();
            closedList.addAll(tempSet);
        };}

    };}

        //Coordinate got add many times
        for(int m = 0;m<closedList.size();m++){
            if(closedList.get(m).getCoord().getX() != closedList.get(m).getParent().getX() || closedList.get(m).getCoord().getY() != closedList.get(m).getParent().getY()){
            System.out.println("X"+closedList.get(m).getCoord().getX()+"Y"+closedList.get(m).getCoord().getY() +"Parent X" + closedList.get(m).getParent().getX() + "Y" + closedList.get(m).getParent().getY());
        }else{
            System.err.println("X"+closedList.get(m).getCoord().getX()+"Y"+closedList.get(m).getCoord().getY() +"Parent X" + closedList.get(m).getParent().getX() + "Y" + closedList.get(m).getParent().getY());

            }
        };
        System.out.println(closedList.size());
        System.out.println(arrive);
        //return closedList;
        return arrive;
    }
}

Class Adjacent.java

public static ArrayList<Node> findAdjacent(Coord coord){
    ArrayList<Node> adj = new ArrayList<Node>();

    if(coord.getX() > 1){
        Node left = new Node(new Coord(coord.getX()-1, coord.getY()), coord);
        adj.add(left);
    }
    if(coord.getX() < 25){
        Node right = new Node(new Coord(coord.getX()+1, coord.getY()), coord);
        adj.add(right);
    }
    if(coord.getY() > 1){
        Node up = new Node(new Coord(coord.getX(), coord.getY()-1), coord);
        adj.add(up);
    }
    if(coord.getY() < 25){
        Node down = new Node(new Coord(coord.getX(), coord.getY()+1), coord);
        adj.add(down);
    }
    return adj;

}

评论中的那些意味着我将在以后修复此问题时添加。谢谢你的帮助。

[编辑]
我现在尝试了@Dhiraj关于改变比较坐标的比较方法的建议。但结果仍然相同,坐标不止一次添加到搜索队列。我还清理了一下代码。

Class Search.java

package pathFinding;

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class Search {
public static Boolean pathFind(Coord start, Coord end /*, ArraList<Coord> blocked*/){

        boolean arrive = false;
        boolean way = false;

        ArrayList<Node> openList = new ArrayList<Node>();
        ArrayList<Node> closedList = new ArrayList<Node>();
        ArrayList<Node> adjNode = new ArrayList<Node>();
        ArrayList<Coord> emptyList = new ArrayList<Coord>();

        int num = 0;
        Node startNode = new Node(start, start);
        closedList.add(startNode);
        openList.add(startNode);
        System.out.println(openList.get(0).getCoord().getX() + " " + openList.get(0).getCoord().getY());
        a:{while(arrive == false){
            if(CheckList.checkCoordInNodeArray(openList, end) || CheckList.checkCoordInNodeArray(closedList, end)){
                arrive = true;
                break;
            }
            all:{for(int i = 0;i<openList.size();i++){
                adjNode.addAll(Adjacent.findAdjacent(openList.get(0).getCoord()));
                for(int j=0;j<adjNode.size();j++){
                    if(CheckList.checkCoordInNodeArray(adjNode, end)){
                        arrive = true;
                        break all;
                    }
                    if(CheckList.checkCoordInNodeArray(openList, adjNode.get(0).getCoord()) == false || CheckList.checkCoordInNodeArray(closedList, adjNode.get(0).getCoord()) == false){
                        System.out.println("o " + CheckList.checkCoordInNodeArray(openList, adjNode.get(0).getCoord()) + " X" + adjNode.get(0).getCoord().getX() + "Y" + adjNode.get(0).getCoord().getY());
                        System.out.println("c " + CheckList.checkCoordInNodeArray(closedList, adjNode.get(0).getCoord())+ " X" + adjNode.get(0).getCoord().getX() + "Y" + adjNode.get(0).getCoord().getY());
                        openList.add(adjNode.get(0));
                        adjNode.remove(0);
                        break;
                    }else{

                        adjNode.remove(0);
                        break;
                    }
                }
                closedList.add(openList.get(0));
                openList.remove(0);
                LinkedHashSet<Node> tempSet = new LinkedHashSet<Node>();
                tempSet.addAll(closedList);
                closedList.clear();
                closedList.addAll(tempSet);
            };}

        };}

        //Coordinate got add many times
        System.out.println("CSIZE" + closedList.size());
        for(int m = 0;m<closedList.size();m++){
            if(closedList.get(m).getCoord().getX() != closedList.get(m).getParent().getX() || closedList.get(m).getCoord().getY() != closedList.get(m).getParent().getY()){
                System.out.println("X"+closedList.get(m).getCoord().getX()+"Y"+closedList.get(m).getCoord().getY() +"Parent X" + closedList.get(m).getParent().getX() + "Y" + closedList.get(m).getParent().getY());
            }else{
                System.err.println("X"+closedList.get(m).getCoord().getX()+"Y"+closedList.get(m).getCoord().getY() +"Parent X" + closedList.get(m).getParent().getX() + "Y" + closedList.get(m).getParent().getY());

            }
        };
        System.out.println(closedList.size());
        System.out.println(arrive);
        //return closedList;
        return arrive;
    }
}

[EDIT2] 这是我的 CheckList.java

package pathFinding;

import java.util.ArrayList;

public class CheckList {

    public static boolean checkNodeArray(ArrayList<Node> ar, Node no){
        boolean ava = true;
        for(Node node : ar){
            if(no.equals(node.getCoord())){
                ava  = true;
            }else{
                ava = false;
            }
        }
        return ava;
    }

    public static boolean checkCoordInNodeArray(ArrayList<Node> ar, Coord Coor){
        boolean ava = true;
        for(Node node : ar){
            if(Node.compareNodeCoord(Coor, node)){
                ava  = true;
            }else{
                ava = false;
            }
        }
        return ava;
    }

    public static Coord findCoordInNodeArray(ArrayList<Node> ar, Coord Coor){
        Coord coor = Coor;
        for(Node node : ar){
            if(Node.compareNodeCoord(Coor, node) == true){
                coor = node.getParent();
                break;
            }else{

            }
        }
        if(coor == Coor){
            System.err.println("NotFound");
        }
        return coor;
    }
}

这是我的compareNodeCoord()

public static boolean compareNodeCoord(Node node1, Node node2){
        boolean equal = false;
        if(node1.equals(node2.getCoord())){
            equal = true;
        }else{
            equal = false;
        }
        return equal;
    }
    public static boolean compareNodeCoord(Coord coor1, Node node2){
        boolean equal = false;
        if(coor1.equals(node2.getCoord())){
            equal = true;
        }else{
            equal = false;
        }
        return equal;
    }

这个问题不是很大,但是当我开始增加行进的块的大小时,程序将停留在非常长的循环中。

对不起,如果这看起来很乱。我是非常新的,也是使用Java编程的新手。

2 个答案:

答案 0 :(得分:0)

您应该覆盖equals方法来比较对象。  请参考以下代码。

public static void main(String[] args) {
        Coordinates c1 = new Coordinates(10, 15);
        List<Coordinates> coordinatesList = new ArrayList<>();
        coordinatesList.add(c1);

        Coordinates c2 = new Coordinates(10, 15);
        for(Coordinates coordinate:coordinatesList ){
        if (coordinate.equals(c2)) {
            System.out.println("Equal ");
        } else {
            System.out.println("Not Equal ");
        }
        }
    }

坐标课程如下

class Coordinates{
    private double x, y;    

    public Coordinates(double x, double y) {
        this.x= x;
        this.y= y;
    }

     // Overriding equals() to compare two Complex objects
    @Override
    public boolean equals(Object o) {

        // If the object is compared with itself then return true  
        if (o == this) {
            return true;
        }

        /* Check if o is an instance of Complex or not
          "null instanceof [type]" also returns false */
        if (!(o instanceof Coordinates)) {
            return false;
        }

        // typecast o to Complex so that we can compare data members 
        Coordinates c = (Coordinates) o;

        // Compare the data members and return accordingly 
        return Double.compare(x, c.x) == 0
                && Double.compare(y, c.y) == 0;
    }
}

答案 1 :(得分:-1)

使用Set而不是List。不,如果将再次需要。在你自己的对象上确保它们有自己的hashCode和equals方法。