如何添加元素并对迭代的数组进行排序?

时间:2016-12-31 17:39:45

标签: java

请帮助您解决此问题? 我有一个节点的ArrayList,它根据距离的比例按升序排序。然后我开始逐个将它们分配给depots,从ArrayList上的第一个客户开始,具有最小比率。在分配节点的过程中也考虑了仓库的容量。此外,不允许违反车厂的容量。请注意,所有节点的总需求小于或等于容量。

在分配过程中,如果某个节点无法分配到仓库,则需要在从第二个最近的仓库到第三个最近的仓库的同时重新计算其比率。最后,将新比率插入到ArrayList中并再次对其进行排序,从而完成了分配过程。

那么,ArrayList在迭代的时候,我想添加一个新元素并对它进行排序吗? 这是代码。 非常感谢,

主要类别:

public static void main(String[] args) {
        ArrayList<Depot> depots = new ArrayList<Depot>();
        Depot A = new Depot(1, 80);         
                depots.add(A);
        Depot B = new Depot(2, 100);        
                depots.add(B);
        Depot C = new Depot(3, 160);        
                depots.add(C);

        ArrayList<Node> nodes = new ArrayList<Node>();
        Node n1 = new Node(1, 17, A, 9, B, 20, C, 30);
        nodes.add(n1);
        Node n2 = new Node(2, 18, A, 17, C, 23, B, 30);
        nodes.add(n2);
        Node n3 = new Node(3, 13, A, 10, B, 21, C, 42);
        nodes.add(n3);
        Node n4 = new Node(4, 19, A, 5, B, 24, C, 33);
        nodes.add(n4);      
        Node n5 = new Node(5, 12, A, 3, B, 30, C, 41); 
        nodes.add(n5);
        Node n6 = new Node(6, 18, B, 6, A, 23, C, 30);
        nodes.add(n6);
        Node n7 = new Node(7, 13, A, 19, B, 27, C, 53);
        nodes.add(n7);
        Node n8 = new Node(8, 13, B, 4, A, 26, C, 30);
        nodes.add(n8);
        Node n9 = new Node(9, 17, C, 19, A, 24, B, 19);
        nodes.add(n9);
        Node n10 = new Node(10, 20, C, 5, A, 36, B, 36);
        nodes.add(n10);
        Node n11 = new Node(11, 16, B, 11, C, 24, A, 25);
        nodes.add(n11);
        Node n12 = new Node(12, 18, A, 11, B, 19, C, 29);
        nodes.add(n12);
        Node n13 = new Node(13, 15, A, 4, B, 31, C, 39);
        nodes.add(n13);
        Node n14 = new Node(14, 11, B, 17, A, 18, C, 23);
        nodes.add(n14);
        Node n15 = new Node(15, 18, C, 16, B, 20, A, 25);
        nodes.add(n15);
        Node n16 = new Node(16, 16, C, 10, B, 24, A, 32);
        nodes.add(n16);
        Node n17 = new Node(17, 15, A, 19, C, 22, B, 32);
        nodes.add(n17);
        Node n18 = new Node(18, 15, A, 7, B, 29, C, 32);
        nodes.add(n18);
        Node n19 = new Node(19, 15, B, 18, C, 33, A, 43);
        nodes.add(n19);
        Node n20 = new Node(20, 16, A, 10, C, 32, B, 33);
        nodes.add(n20);

        realtive(depots, nodes);
        for(Depot d : depots) {
            System.out.println("Depot No. : " + d.getId());
            for(Node n : d.getNodeList()) {
                System.out.print(n.getId() + ", ");
            }
            System.out.println("");
        }
    }

    public static void realtive(ArrayList<Depot> depots, ArrayList<Node> nodes) {
        ArrayList<Node> newList = new ArrayList<Node>();
        for(Node n : nodes) {
            if(n.getIsAssigned() == false && n.getDegree() == 1) {              
                double ratio = (double) n.getDistance_1() / n.getDistance_2();              
                n.setRatio(ratio);
                newList.add(n);
            }
            if(n.getIsAssigned() == false && n.getDegree() == 2) {
                double ratio = (double) n.getDistance_2() / n.getDistance_3();
                n.setRatio(ratio);
                newList.add(n);
            }
            if(n.getIsAssigned() == false && n.getDegree() == 3) {
                double ratio = (double) n.getDistance_3() / n.getDistance_3();
                n.setRatio(ratio);
                newList.add(n);
            }
        }

        Collections.sort(newList);

        boolean isAssigned = false;
        for(Node n : newList) {
            for(Depot d : depots) {
                isAssigned = assignNode(n, d, n.getDegree());
            }
            if(isAssigned = false)
                break;
        }

        int count = 0;
        for(Node n : nodes) {
            if(n.getIsAssigned() == false) {
                count = count + 1;
            }
        }
        if(count < nodes.size()){
            realtive(depots, nodes);
        }
    }

    public static boolean assignNode(Node n, Depot d, int i) {
        boolean isAssigned = false;
        if(n.getDegree() == 1) {
            if(n.getIsAssigned() == false && n.getDepot_1().equals(d) && n.getDemand() <= d.getCapacity()) {
            d.addNode(n);
            n.setIsAssigned(true);
            isAssigned = true;
            } else {
                n.increaseDegree();
                isAssigned = false;
            }
        }
        if(n.getDegree() == 2) {
            if(n.getIsAssigned() == false && n.getDepot_2().equals(d) && n.getDemand() <= d.getCapacity()) {
            d.addNode(n);
            n.setIsAssigned(true);
            isAssigned = true;
            } else {
                n.increaseDegree();
                isAssigned = false;
            }
        }
        if(n.getDegree() == 3) {
            if(n.getIsAssigned() == false && n.getDepot_3().equals(d) && n.getDemand() <= d.getCapacity()) {
            d.addNode(n);
            n.setIsAssigned(true);
            isAssigned = true;
            } else {
                n.increaseDegree();
                isAssigned = false;
            }
        }
        return isAssigned;
    }

基地类:

public Depot(int myId, int myCapacity) {
    id = myId;
    capacity = myCapacity;
    nodeList = new ArrayList<Node>();
}

public void addNode(Node n) {
    nodeList.add(n);
    capacity -= n.getDemand();
}

public int getId() { return id; }
public int getCapacity() { return capacity; }
public List<Node> getNodeList() { return nodeList; }

0 个答案:

没有答案