Java 数组列表、引用和;递归

Java 数组列表、引用和;递归,java,recursion,arraylist,vector,Java,Recursion,Arraylist,Vector,我正在做一个项目,需要使用递归和ArrayList/vectors来解决一个3-可满足性实例。任务是选择一个子集的整数,使得下面表示的每个集合包含该子集合的一个或多个元素。约束条件是,一个数字i及其对立面-i不能同时在子集中 Vector<Vector<Integer>> matrix = new Vector<Vector<Integer>>(); for(int i=0;i<4;i++) { matrix.add(new Vect

我正在做一个项目,需要使用递归和ArrayList/vectors来解决一个3-可满足性实例。任务是选择一个子集的整数,使得下面表示的每个集合包含该子集合的一个或多个元素。约束条件是,一个数字i及其对立面-i不能同时在子集中

Vector<Vector<Integer>> matrix = new Vector<Vector<Integer>>();
for(int i=0;i<4;i++)
{
    matrix.add(new Vector<Integer>());
}
程序返回到选择3的阶段,理想情况下现在选择4作为下一个字母。但我删除了整行

有没有一种方法可以在不创建未知数量向量的情况下实现这一点


第二,数组列表和向量之间是否存在显著差异?我一直在独家使用ArrayList,但该项目建议使用向量。

你问题的第一部分不连贯。不过,我将详细说明Vector和Arraylist之间的区别:

  • 如果多个线程同时访问ArrayList,则需要从外部同步修改列表的代码块
  • 在内部,它们都使用一个数组来维护数据结构。如果空间不足,ArrayList会将其大小增加50%,而Vector会将其大小增加一倍

请参阅thread以获得详细解释。

您知道什么是递归吗?ArrayList和Vector之间没有显著差异,但在实际场景中我建议使用ArrayList。请参阅,以了解有关微小差异的详细信息。我不明白您打算如何通过递归来处理这个问题,或者问题是什么。你能提供一个粗略的例子吗?“但我希望原始版本保持不变。有没有办法做到这一点而不创建未知数量的向量?”没有。向量也比Arraylist慢得多,因为它需要同步。
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.