-1

これに対して得られる出力は次のとおりです。

Inside loop : [5, 6, 3, 1, 4, 2]

Inside loop : [3, 1, 5, 6, 4, 2]

Inside loop : [1, 2, 4, 5, 6, 3]

Inside loop : [5, 2, 6, 4, 3, 1]

Outside loop : [5, 2, 6, 4, 3, 1]

Outside loop : [5, 2, 6, 4, 3, 1]

Outside loop : [5, 2, 6, 4, 3, 1]

Outside loop : [5, 2, 6, 4, 3, 1]

コード:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PossibleSolution {
    // the indices of where to place the cuts to delimit routes (different
    // vehicles)
    int[] indicesCut;
    // the set of ordered Customers for each route. Routes delimited by cuts
    ArrayList<Integer> OrderedCustomers;
    // length of array
    int size;

    // Constructor
    public PossibleSolution(int[] indices, ArrayList<Integer> Customers) {
        this.indicesCut = indices;
        this.OrderedCustomers = Customers;

        this.size = Customers.size();
    }

    // method to generate the neighborhood for one possible solution. We need a
    // parameter
    // to specify the number of neighbors to generate

    public PossibleSolution[] generateNeighborhood(int number) {
        PossibleSolution[] sol = new PossibleSolution[number];
        for (int i = 0; i < number; i++) {
            java.util.Collections.shuffle(this.OrderedCustomers);

            sol[i] = new PossibleSolution(this.indicesCut, this.OrderedCustomers);
            System.out.println("Inside loop : " + sol[i].OrderedCustomers);
        }

        for (int i = 0; i < number; i++) {
            System.out.println("Outside loop : " + sol[i].OrderedCustomers);
        }

        return sol;
    }

    public static void main(String[] args) {
        ArrayList<Integer> Customers = new ArrayList();
        Customers.add(2);
        Customers.add(4);
        Customers.add(5);
        Customers.add(1);
        Customers.add(6);
        Customers.add(3);
        int[] ind = { 2, 3 };
        PossibleSolution initialSol = new PossibleSolution(ind, Customers);
        PossibleSolution[] table = initialSol.generateNeighborhood(4);
    }
}
4

3 に答える 3

4

あなたPossibleSolutionのすべてが同じを参照していますArrayList

(すべての変数とフィールドは、 で作成しArrayListた単一のものを指しています。したがって、リストをシャッフルするたびに、あらゆる場所のリスト値に影響します。リストの状態のスナップショットを取得したい場合呼び出されたので、コピーを作成する必要があります。)ArrayListmain()PossibleSolution()

于 2013-11-08T17:10:10.897 に答える
1

概要

コンストラクターは Customers をコピーせず、それへの参照を格納するだけです。そのため、1 つのオブジェクトへの参照を複数PossibleSolutionのに渡すと、それらはすべてそれを共有します。

public PossibleSolution(int[]indices, ArrayList<Integer> Customers){
    this.indicesCut = indices;
    this.OrderedCustomers = Customers; //<-- Only the reference is copied, not the object

    this.size = Customers.size();
}

説明

for(int i =0; i<number;i++){        
        java.util.Collections.shuffle(this.OrderedCustomers);

        sol[i] = new PossibleSolution(this.indicesCut,this.OrderedCustomers);
        System.out.println("Inside loop : "+sol[i].OrderedCustomers);
    }

すべてPossibleSolutionの s は同じものを共有するため、this.OrderedCustomersシャッフルするたびに、すべてのs のthis.OrderedCustomers内部構造が変更されますPossibleSolution

したがって、これが同じものを何度も印刷することは驚くべきことではありません

for(int i=0; i<number;i++){
   System.out.println("Outside loop : "+sol[i].OrderedCustomers);
}

同じOrderedCustomersであるため

解決

コピーが必要な場合は、参照だけでなくオブジェクトのコピーを要求する必要があります。これを行う最も簡単な方法は、System.arrayCopyを使用することです。

System.arraycopy(from, 0,to,0,from.length);

参考文献

この同じ「異なる場所にある同じオブジェクトへの参照」問題の簡略版は、ここにあります


その他の注意事項

OrderedCustomersCustomersは両方とも変数であるため、lowerCamelCase にする必要があります。orderedCustomerscustomers

于 2013-11-08T17:13:13.667 に答える
0

「外側のループ」とラベル付けされたすべての印刷ステートメントは、常に同じ配列に対して実行されます。for最初のループを抜けた後は、何もランダム化していません。あなたは何度も何度も印刷しています。

于 2013-11-08T17:11:57.980 に答える