My code should merge two already sorted arraylists into one sorted arraylist and if one of the arraylists used is not sorted then it should return null.
public class MergeSorted {
public static void merge(ArrayList<Integer> a, ArrayList<Integer> b) {
for (int i = 0, j = 0; j < b.size(); i++) {
if (i == a.size() || a.get(i) > a.get(j)) {
a.add(i, b.get(j++));
}
}
}
}
This is what I attempted but can't get the idea of returning null if they are not equal, Im new to java and this is my second week so please be patient with me. I know I should have an if statement checking if they are sorted and an else but what should I include inside the if?
equals()andcompare()you could order the result list usingCollections.sort()nullimplies that you should be creating new array list to store merged result, not modifying source array list.it should return nullFirst of all you are not returning anything i.e. void.TreeSetand insert the contents of both lists there. Using aSetmerges the data for you so that duplicates are removed. Or do you want to keep duplicate entries?