2016-04-07 9 views
0

私は、3つの条件を満たすインスタンスを解決するために、再帰とarraylists/vectorsを使う必要があるプロジェクトに取り組んでいます。タスクは、整数の部分集合を選択することであり、下に示す各集合は、部分集合の1つ以上の要素を含む。制約は、番号i &の反対の-iを両方ともサブセットに含めることができないという制約です。 2次元ベクトル/ ArrayListには、その後、例えば、数字で満たされているArrayLists、References、&Recursion

Vector<Vector<Integer>> matrix = new Vector<Vector<Integer>>(); 
for(int i=0;i<4;i++) 
{ 
    matrix.add(new Vector<Integer>()); 
} 

1, -2, 3 
-1, 2, 4 
2, -3, 4 
-1, -2, -3 

if(startRec(matrix) 
{ 
    //solution is found 
} 
else 
{ 
    //no solution is possible 
} 


private boolean startRec(Vector<Vector<Integer>> matrix) 
{ // I'm using trial and error to find a solution to the above problem. So in the below, matrix.get(0).get(i) is selected as part of a possibly correct set. 
    boolean success=false; 
    if(stagen(matrix.get(0).get(0), matrix)) 
    { 
     success=true; 
    } 
    else if(stagen(matrix.get(0).get(1),matrix)) 
    { 
     success=true; 
    } 
    else if(stagen(matrix.get(0).get(2),matrix)) 
    { 
     success=true; 
    } 

    return success; 
} 
private boolean stagen(int input, Vector<Vector<Integer>> matrix) 
{ 
    removei(input, matrix) // this removes all Vector<Integer> that contain i. Those sets are considered satisfied, and no longer need to be addressed. 
    removenegative(input,matrix) // this removes all integers that are -input. Since i and -i cannot both be selected, I'm removing the -input. 
//So if a set contained three ints, one of which was -input, it now contains 2. 
    boolean success=false; 
    if(stagen(matrix.get(0).get(0), matrix)) // since the original matrix.get(0) contained input, it was removed in removei(input,matrix), thus this is a one below the one called previously. 
    { 
     success=true; 
    } 
    else if(stagen(matrix.get(0).get(1),matrix)) 
    { 
     success=true; 
    } 
    else if(stagen(matrix.get(0).get(2),matrix)) 
    { 
     success=true; 
    } 

    return success; 
} 

私はそれを読みやすくするために、範囲チェックのアウトを削除しました。しかし、私が起こっている確信していますプロセスはこれです:

1, -2, 3 //1 is selected in the second line of startRec 
-1, 2, 4 
2, -3, 4 
-1, -2, -3 

//1, -2, 3 line is removed from consideration by method removei() as 1 has been selected 
2, 4  // -1 has been removed as both 1,-1 cannot be selected. 
2, -3, 4 
-2, -3  // -1 removed. 

2, 4  now 2 is the first number, so 2 is selected. 
-2, 3, 4 
-2, -3 

//2, 4  removed via removei method. 
3, 4  //-2 is removed, because 2 has been selected. 
-3   //-2removed. 

3, 4 //Now 3 is selected. 
-3 

//3, 4 line removed as it has been satisfied. 
_____ //There's now an empty set here as -3 was deleted 
//false is returned by stagen. 

プログラムは3を選択したstagenに戻り、それが今、理想的になりました次の文字として4を選択します。しかし、私はその行全体を削除しました。

未知数のベクトルを作成せずにこれを行う方法はありますか?

第2に、アライリスト&ベクターには大きな違いがありますか?私はarraylistsを独占的に使ってきましたが、このプロジェクトはベクターを提案しています。

+0

あなたは再帰が何であるか知っていますか? –

+2

ArrayListとVectorの間に大きな違いはありませんが、実際のシナリオではArrayListをお勧めします。副次的な違いの詳細については、http://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vectorを参照してください。 – Krease

+2

再帰によってこれを操作する方法を理解していません。何が問題なの?大まかな例を教えてください。 – Krease

答えて

0

質問の最初の部分はインコヒーレントです。しかし、私は、VectorとArrayListの間の違いについて詳しく説明します:

  • を複数のスレッドが同時に、その後のArrayListにアクセスするあなたは、外部リスト変更するコードのブロックを同期する必要がある場合。
  • 内部的には、両方ともデータ構造を維持するために配列を使用します。スペースを使い果たすと、ArrayListはVectorのサイズを倍にする一方、ArrayListはそのサイズを50%増加させます。

詳細な説明はthisスレッドを参照してください。

+0

ベクターは、その同期要件のためにArraylistよりもはるかに低速です。 –

関連する問題