This is the merge part of my solution. Method merge
merges two already sorted lists. I feel the code I wrote isn't the best implementation. Please review the code and tell me where I can improve.
public static List<Integer> merge(List<Integer> tomerge1,List<Integer> tomerge2){
int n = tomerge1.size();
int m = tomerge2.size();
List<Integer> result = new ArrayList();
int i=0;
int j=0;
while(i<n){
while(j<m && tomerge1.get(i)>tomerge2.get(j)){
result.add(tomerge2.get(j));
j++;
}
result.add(tomerge1.get(i));
i++;
try {
tomerge1.get(i); // If all the elements of 1 are added to the
// result and some elements of the 2nd remain,
// this line will give IndexOutOfRange error.
}catch (Exception e){
while(j<m){
result.add(tomerge2.get(j));
j++;
}
}
}
return result;
}