java - Recursive implementation of a method -


i'm new java , still trying wrap head around recursion.the function below returns true @ first intersection between 2 sorted lists list x , list y.

public static boolean checkintersection(list<integer> x, list<integer> y) {     int = 0;     int j = 0;     while (i < x.size() && j < y.size()) {         if (x.get(i).equals(y.get(j))) {             return true;         } else if (x.get(i) < y.get(j)) {             i++;         } else {             j++;         }     }     return false; } 

now i've been trying implement using recursion instead, , know there should base case empty list in case , try reduce list excluding 1 element @ time , feed same recursive function, can't work out how check intersection pass rest of list on , over.

public static boolean recursivechecking(list<integer> x,list<integer> y) {     if(x.size() == 0){         return false;     }     else {         return recursivechecking(x.sublist(1, x.size()-1), y);     } } 

any highly appreciated. thank you.

as lists ordered, recursion should remove first element of list smaller first value. have return true, if both lists start same number , false if of lists empty. otherwise keep removing elements. (this code untested):

public static boolean recursivechecking(list<integer> x,list<integer> y) {     if(x.size() == 0 || y.size() == 0){         return false;     } else if (x.get(0).equals(y.get(0))) {         return true;     } else {         if (x.get(0) < y.get(0)) {             return recursivechecking(x.sublist(1, x.size()-1), y);         } else {             return recursivechecking(x, y.sublist(1, y.size()-1));         }     } } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -