SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old November 27th, 2001, 12:13 AM
fancy03 fancy03 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Atlanta, GA
Posts: 0 fancy03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old November 27th, 2001, 12:18 PM
zhunter zhunter is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Boston, Ma
Posts: 0 zhunter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #3  
Old November 27th, 2001, 12:55 PM
fancy03 fancy03 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Atlanta, GA
Posts: 0 fancy03 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks!!! That was perfect!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Merging arrays in Java


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway