我正在尝试创建一个程序,它可以获得课程的所有先决条件,我还需要获得先决条件的先决条件,直到我得到一个没有任何先决条件的课程并将所有课程存储在一个数组中。 到目前为止,我尝试这段代码:
public class MainFunctions {
public static int[] getPrerequisites(int value){
ArrayList<Integer> obj = new ArrayList<Integer>();
Datasource ds=new Datasource();
try{
ds.connect();
java.sql.Statement s=ds.createStatement();
int[] arr1 = ArrayUtils.toPrimitive(obj.toArray(new Integer[obj.size()]));
ResultSet rs = s.executeQuery("Select prerequisites_course_id from common_course_prerequisites where course_id="+value);
while(rs.next()){
int a =rs.getInt(1);
boolean contains = IntStream.of(arr1).anyMatch(x -> x == a);
if(contains==false){
obj.add(rs.getInt(1));
MainFunctions.getPrerequisites(rs.getInt(1));
}
}
ds.dropConnection();
} catch (Exception ex) {
ex.printStackTrace();
}
int[] arr = ArrayUtils.toPrimitive(obj.toArray(new Integer[obj.size()]));
System.out.println(obj);
return arr;
//boolean contains = IntStream.of(arr).anyMatch(x -> x == a);
}
}