Im trying to learn java by myself and one of the questions in the book is handing my but to me. It's about merging two ArrayList after sorting them MergeSort style. I can't merge them back together without a major calamity. I really want to know it so I could move on, it's driving me nuts.
public <E extends Comparable<?super E>> void merge(List<E> front, List<E> back, boolean last, List<E> result){
int i = 0;
int j = 1;
while (!front.isEmpty() && !back.isEmpty()) {
if (front.get(0).compareTo(back.get(0)) <= 0) {
result.add(result.size() -1, front.remove(0));
System.out.println("works" + result);
} else {
result.add(result.size() -1, back.remove(0));
System.out.println("no work" + result);
}
}
while (!front.isEmpty()) {
result.add(result.size() -1,front.remove(0));
}
while (!back.isEmpty()) {
result.add(result.size() - 1, back.remove(0));
}
System.out.println();
} }
The boolean value is to sort them in: true==ascending order, false==descending order. I could worry about that. Any type of help will be appreciated.