#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Atlanta, GA
    Posts
    0
    Rep Power
    0

    Merging arrays in Java


    I need help merging two arrays in java. Sizes may vary. This is the method:
    public static int [] merge(Comparable[] arr1, Comparable[]arr2)

    The arrays are both already in asending order.
    thanks
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Boston, Ma
    Posts
    0
    Rep Power
    0
    Well, there's basically two ways to go about this. It all depends on whether you need to keep multiple copies of the same value in the merged array. That is if you have [1,2,3] and [2,4,6] should the result be [1,2,3,4,6] or [1,2,2,3,4,6]?

    If you don't mind losing the duplicates, the easiest way is something like this:
    Code:
     public static int[] merge(Comparable[] arg1, Comparable[] arg2)
     {
        //convert arrays to collections (lists)
        Collection coll1 = Arrays.asList(arg1);
        Collection coll2 = Arrays.asList(arg2);
    
        //Create a SortedSet from the first collection
        SortedSet sorter = new TreeSet(coll1);
    
        //Add the second collection
        sorter.addAll(coll2);
    
        //Create an array to hold the results
        int[] merged = new int[sorter.size()];		
        Iterator itSorted = sorter.iterator();
    
        for (int i = 0; itSorted.hasNext(); i++) {
            merged[i] = ((Integer)itSorted.next()).intValue();
        }
    
        //return the SortedSet as an array
        return(merged);
     }
    The only complication is converting your Comparable objects into int values. The code above assumes that all elements of both arrays are actually instances of Integer. If that's not the case, you'd have to do some other conversion. This option, though easy to code, is not the most efficient.

    If you want to keep the duplicates or you're overly concerned about performance, you could try something like this:

    Code:
     public static int[] merge(Comparable[] arg1, Comparable[] arg2)
     {
        Comparable[] rest;
        int i1, i2, iRest, iMerged;		
    
        //Create a new array to hold the merged elements
        int[] merged = new int[arg1.length + arg2.length];
        i1 = i2 = iMerged = 0;
    
        //Step through both arrays comparing elements as you go.
        //When you insert one element into the merged array, step to the
        // next element in that array.
        while(i1 < arg1.length && i2 < arg2.length) {
    
            if(arg1[i1].compareTo(arg2[i2]) < 0) {
                merged[iMerged++] = ((Integer)arg1[i1++]).intValue();
            }else {
                merged[iMerged++] = ((Integer)arg2[i2++]).intValue();
            }
        }
    	    
        //Figure out which array is done
        if(i1 == arg1.length) {
            iRest = i2;
            rest = arg2;
        } else {
            iRest = i1;    
            rest = arg1;
        }
    	    
        //Now just dump in the rest of the remaining array contents	into 
        // the new array   
        while (iRest < rest.length) {
            merged[iMerged++] = ((Integer)rest[iRest++]).intValue();
        }
    	    
        return(merged);
    
     }
    I hope this helps.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Atlanta, GA
    Posts
    0
    Rep Power
    0
    Thanks!!! That was perfect!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Thanks!!!


    Tremendous help with a homework problem that was killing me...

IMN logo majestic logo threadwatch logo seochat tools logo