IC2D
IC2D

Reputation: 501

Can't merge two arraylists

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.

Upvotes: 0

Views: 207

Answers (3)

prajeesh kumar
prajeesh kumar

Reputation: 1966

Well, one safe method I used to implement the merging was something like the following.

public <E extends Comparable<? super E>> void merge(List<E> front,
            List<E> back, boolean last, List<E> result) {
        int i = 0;
        int j = 0;
        Collections.sort(front);
        Collections.sort(back);
        while (!(i == front.size() && j == back.size())) {
            E e;
            if (i == front.size()) {
                // If we have exhausted the 'front' then copy whatever is in
                // 'back'
                e = back.get(j);
                // Move back index to next
                j++;
            } else if (j == back.size()) {
                // If we have exhausted the 'back' then copy whatever is in
                // 'front'
                e = front.get(i);
                // Move front index to next
                i++;
            } else {
                if ((front.get(i).compareTo(back.get(j)) <= 0)) {
                    // front item will be added, increment front index for next
                    e = front.get(i);
                    i++;
                } else {
                    // back item will be added, increment back index for next
                    e = back.get(j);
                    j++;
                }
            }
            result.add(e);
        }
        if(last){
            Collections.reverse(result);
        }
    }

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

I think all you have to do is make the following change

result.add(front.remove(0));

and the same change in the else clause.

To add an element at the end of the result list you shouldn't specify an index.

Upvotes: 1

Adrian
Adrian

Reputation: 5681

If your arraylists are sorted, loop through them and just take the minimum element at one end (assuming you want ascending order). Do this until one of the lists is empty. Then just append the entries in the leftover list to the merged list.

Upvotes: 0

Related Questions