0

N 個の d 次元の arraylist を 1 つの大きな arraylist に連結したいと考えています。私は次のことをしました:

 {
     Arraylist<Double> cFeature = new Arraylist<Double>(); 
     for(int i =0 ; i < n; i++){
        ArrayList<Double> arrList = new ArrayList<Double>();
        arrList = FeatureMatrix.get(i);
        cFeature.addAll(arrList);
     }  
    return cFeature;
}

しかし、これは連結された特徴ベクトルを返しません。その理由は何ですか?

次のタイプのマトリックスがあります

    1 2 3 4
    4 5 6 7
    9 4 5 2

次のようなものを返したい:

    12344567, 45679452 etc

EDIT[行列 1 行],行列の次元は 50

   [-1.4707351089135285, -2.396807707665656, -0.9225858560474335, -0.552093667789784, 0.6492206869123566, 1.1347653279780474, 0.18599226559979623, -0.3040490372134513, -0.8661743997164002, 1.2990217001062885, -1.4689261255216413, -0.6175058675322327, 0.0019875740898560707, 3.187991109660048, 0.9793588130569899, 1.88726260031087, 1.263110196592273, 0.10270882950413489, -0.33850097448844163, 0.26780865103769547, -0.28117099016766645, -0.015511886681809741, -0.7906057240014217, 0.1796874905794462, 0.9327631100459427, 0.5419684468033518, 1.3670537985364393, -1.0381888770019032, 1.0975151287297011, 0.024367745998744996, -0.25780912155025204, -1.862325174655491, -0.611104255824939, -0.5746070435381269, -1.2218773341433158, 0.2220916817954159, 0.4641455500389115, -0.43253367269335635, -0.5380163044326588, 0.685592907063921, -0.6191939669845238, -1.2275198581496152, 0.13270110767423787, -0.1614948461888469, 1.5717904793822337, -0.2826323802880358, -0.4716922630198008, -0.2881374794211655, 0.8609910302314909, 1.1749707572533885]
4

1 に答える 1

1

質問のコードの重要な部分を省略しました。チャットpastebinの完全なコードを見ると、Javaで割り当てがどのように機能するかを理解していないことが問題のようです(おそらくC ++のバックグラウンドがありますか?)

    ArrayList<Double> contextFeature = new ArrayList<Double>();
    contextFeature = wordFeatureMatrix.get(x); // x is a valid integer key, actually FrequentWordIndex.get(Ngram.get(0).toLowerCase()) in the pastebin code

これにより、contextFeature用に作成した元のArrayListが破棄され、wordFeatureMatrixのArrayListの1つに置き換えられます。

その後、繰り返し処理するwordFeatureMatrixのではなく、wordFeatureMatrixにインデックスを返すリストを繰り返し処理します。私はある時点でそれを確信しています

wordFeatureMatrix.get(FrequentWordIndex.get(Ngram.get(0).toLowerCase())) ==
    wordFeatureMatrix.get(FrequentWordIndex.get(Ngram.get(i).toLowerCase()));

つまり、後で基本的に電話をかけることになります

 contextFeature.addAll(contextFeature);

JavaDocからArrayList.addAll()

操作の進行中に指定されたコレクションが変更された場合、この操作の動作は未定義です。(これは、指定されたコレクションがこのリストであり、このリストが空でない場合、この呼び出しの動作が未定義であることを意味します。)

したがって、2つの問題があります。wordFeatureMatrix1つ目は、リストを収集する過程で変更しないことですcontextFeature。これを解決策として置き換える必要があります。

    ArrayList<Double> contextFeature = new ArrayList<Double>();
    contextFeature = wordFeatureMatrix.get(x); 

    ArrayList<Double> contextFeature = new ArrayList<Double>();
    contextFeature.addAll(wordFeatureMatrix.get(x)); 

2番目の問題は、ユースケースでは問題にならない可能性がありますが、同じリストが2回追加されないようにすることです。それがあなたが望むものであるかどうかを決めるのはあなた次第です。

于 2012-04-22T06:16:02.793 に答える